How do I replace double quotes with its escape?

RJ.
var string = "{ "Name": ""Jack"" }"

I want to replace the double quotes with \" so that the variable becomes a valid JSON.

so, it should end up looking like this:

string = "{ "Name": \""Jack"\" }"

I know you can use the replace function but I'm not getting it to work.

ThiefMaster

Put a backslash in front of each double quote that should be escaped.

var string = "{\"Name\":\"\\\"Jack\\\"\"}"

However, your questions very much looks like an XY problem where you are trying to do something in the completely wrong way! You usually never have to deal with escaping etc. when JSON is involved.

Initially you probably have an object. Let's assume obj = {Name: "Jack"}. Now you apparently want to JSON-encode it. In JavaScript you use JSON.stringify(obj) for it, in PHP you'd do json_encode($obj). However, if you want to assign this to a JS variable, you can just put the encoded JSON right after obj =, like this. If you really have to put a JSON string somewhere, you can simply run the JSON encoder over the string again (that's how I created the string in this post):

JSON.stringify(JSON.stringify({Name: 'Jack'}))

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How do I correctly pass double quotes to an awk subprocess in Python?

분류에서Dev

how to remove double quotes and escape it in specific parts of a string

분류에서Dev

How can I escape double curly braces in jinja2?

분류에서Dev

How should i replace double space(continuous space) by double characters?

분류에서Dev

How do I add a double tab in fortran?

분류에서Dev

How do I escape the '+' characters in an AutoHotKey, with the date script?

분류에서Dev

How do i replace an element with the children it contains?

분류에서Dev

How to add one double-quotes (") to string?

분류에서Dev

How to give variable as command parameter in double quotes?

분류에서Dev

How do I Alter an attribute to change its datatype in Object type?

분류에서Dev

How do I mock a domain in Grails but get it by its Parent?

분류에서Dev

How do I copy the directory and its folders in windows command line?

분류에서Dev

What is dconf, what is its function, and how do I use it?

분류에서Dev

How do I convert a double variable to an integer variable?

분류에서Dev

grep not working when I enclose the directory in double quotes

분류에서Dev

How do I print a character variable within quotes using php function

분류에서Dev

How do I keep single quotes intact when returning the output of a Bash subshell to the OSX "defaults" utility?

분류에서Dev

DocuSign: How do I dynamically replace placeholders in our source document?

분류에서Dev

android: how do I replace SQLite execSQL() to avoid injection attack?

분류에서Dev

How do I replace a whitespace with any number of whitespaces?

분류에서Dev

How do I change/replace fragment on button press?

분류에서Dev

How do I replace HTML content with a string in python using BeautifulSoup?

분류에서Dev

How do I replace a comma separated list of strings with the first occurrence?

분류에서Dev

How do I replace certain PHP values with my own text?

분류에서Dev

How do I create an AWS ELB listener with frontend security policy? How do I replace the policy?

분류에서Dev

How to use double quotes in bash around a directory with spaces

분류에서Dev

How to add double quotes to a various strings in a same pattern in a file

분류에서Dev

How to read JSON into a JObject when the string contains double quotes ?

분류에서Dev

How to set Double Quotes using VBScript Dictionary in UFT 12.0

Related 관련 기사

  1. 1

    How do I correctly pass double quotes to an awk subprocess in Python?

  2. 2

    how to remove double quotes and escape it in specific parts of a string

  3. 3

    How can I escape double curly braces in jinja2?

  4. 4

    How should i replace double space(continuous space) by double characters?

  5. 5

    How do I add a double tab in fortran?

  6. 6

    How do I escape the '+' characters in an AutoHotKey, with the date script?

  7. 7

    How do i replace an element with the children it contains?

  8. 8

    How to add one double-quotes (") to string?

  9. 9

    How to give variable as command parameter in double quotes?

  10. 10

    How do I Alter an attribute to change its datatype in Object type?

  11. 11

    How do I mock a domain in Grails but get it by its Parent?

  12. 12

    How do I copy the directory and its folders in windows command line?

  13. 13

    What is dconf, what is its function, and how do I use it?

  14. 14

    How do I convert a double variable to an integer variable?

  15. 15

    grep not working when I enclose the directory in double quotes

  16. 16

    How do I print a character variable within quotes using php function

  17. 17

    How do I keep single quotes intact when returning the output of a Bash subshell to the OSX "defaults" utility?

  18. 18

    DocuSign: How do I dynamically replace placeholders in our source document?

  19. 19

    android: how do I replace SQLite execSQL() to avoid injection attack?

  20. 20

    How do I replace a whitespace with any number of whitespaces?

  21. 21

    How do I change/replace fragment on button press?

  22. 22

    How do I replace HTML content with a string in python using BeautifulSoup?

  23. 23

    How do I replace a comma separated list of strings with the first occurrence?

  24. 24

    How do I replace certain PHP values with my own text?

  25. 25

    How do I create an AWS ELB listener with frontend security policy? How do I replace the policy?

  26. 26

    How to use double quotes in bash around a directory with spaces

  27. 27

    How to add double quotes to a various strings in a same pattern in a file

  28. 28

    How to read JSON into a JObject when the string contains double quotes ?

  29. 29

    How to set Double Quotes using VBScript Dictionary in UFT 12.0

뜨겁다태그

보관