conversion between a JSON string and a JS object1 JSON string converted to JS object1.1 Standard JSON format string converted to JS object
JSON string str |
Json.parse (str) |
eval (str) |
Eval (' (' +str+ ') ') |
Eval (' [' +str+ '] ') |
Object String "{\" id\ ": \" 001\ ", \" name\ ": \" sanzhang\ "}" corresponding to the JSON format {"id": "001", "Name": "Sanzhang"} |
Object {ID: "001", Name: "Sanzhang"} |
Syntaxerror:unexpected token: |
Object {ID: "001", Name: "Sanzhang"} |
[Object
- ID: "001"
- Name: "Sanzhang"
- __proto__: Object
] |
Array string "[{\" id\ ": \" 001\ ", \" name\ ": \" sanzhang\ "}]" corresponding to the JSON format [{"id": "001", "Name": "Sanzhang"}] |
[Object
- ID: "001"
- Name: "Sanzhang"
- __proto__: Object
] |
[Object
- ID: "001"
- Name: "Sanzhang"
- __proto__: Object
] |
[Object
- ID: "001"
- Name: "Sanzhang"
- __proto__: Object
] |
[Array[1]
- 0: Object
- ID: "001"
- Name: "Sanzhang"
- __proto__: Object
- Length: 1
- __PROTO__: array[0]
] |
Embedded JSON substring "[{\" id\ ": \" 001\ ", \" name\ ": \" sanzhang\ "},{\" id\ ": \" {\\\ "id1\\\": \\\ "1\\\", \\\ "id2\\\": \\\ "2\\\"}\ "}]" corresponding JSON format [{"id": "001", "Name": "Sanzhang"},{"id": "{\" id1\ ": \" 1\ ", \" id2\ ": \" 2\ "}"}] |
[Object
- ID: "001"
- Name: "Sanzhang"
- __proto__: Object
, Object
- ID: "{" ID1 ":" 1 "," Id2 ":" 2 "}"
- __proto__: Object
] |
[Object
- ID: "001"
- Name: "Sanzhang"
- __proto__: Object
, Object
- ID: "{" ID1 ":" 1 "," Id2 ":" 2 "}"
- __proto__: Object
] |
[object
- id: "001"
- name: " Sanzhang "
- __proto__: object
, Object
- Id: " {"ID1": "1", "Id2": "2" } "
- __proto__: object
] |
[Array[2]
- 0: Object
- ID: "001"
- Name: "Sanzhang"
- __proto__: Object
- 1: Object
- ID: "{" ID1 ":" 1 "," Id2 ":" 2 "}"
- __proto__: Object
- Length: 2
- __PROTO__: array[0]
] |
Inline JSON substring Conversion experiment: Json.parse (Json.parse (str) [1].id) Object {id1: "1", Id2: "2"}eval (Json.parse (str) [1].id)syntaxerror:unexpected token:Eval (' (' +json.parse (str) [1].id+ ') ') object {id1: "1", Id2: "2"} Summary: Standard JSON-formatted string, using Json.parse to convert to JS object is preferred, eval ( JSON object string) error, eval (' (' + ' + ') ') is a good choice, and eval (' [' + ' + ') ') will outsource the JSON string to an array.
1.2 Non-standard JSON format string converted to JS object
JSON string str |
Json.parse (str) |
eval (str) |
Eval (' (' +str+ ') ') |
object key without quotation mark "{id:\" 001\ ", Name:\" sanzhang\ "}" corresponding to the JSON format {ID: "001", Name: "Sanzhang"} |
syntaxerror:unexpected token I |
Syntaxerror:unexpected token: |
Object {ID: "001", Name: "Sanzhang"} |
The object key is single quote "{' id ': \" 001\ ", ' name ': \" sanzhang\ "}" corresponding to the JSON format {' id ': "001", ' Name ': "Sanzhang"}
|
syntaxerror:unexpected token ' |
Syntaxerror:unexpected token: |
Object {ID: "001", Name: "Sanzhang"} |
The value of the object is without quotation marks "{\" id\ ": 001,\" name\ ": Sanzhang}" corresponding to the JSON format {"id": 001, "name": Sanzhang} |
syntaxerror:unexpected number |
Syntaxerror:unexpected token: |
Referenceerror:sanzhang is not defined |
The value of the object is single quotation marks "{\" id\ ": ' 001 ', \" name\ ": ' Sanzhang '}"
The corresponding JSON format {"id": ' 001 ', "name": ' Sanzhang '}
|
syntaxerror:unexpected token ' |
Syntaxerror:unexpected token: |
Object {ID: "001", Name: "Sanzhang"} |
Summary: A non-standard JSON-formatted string, using Json.parse,eval (JSON object string) will make an error, eval (' (' + ' + ') ') is a good choice.
In Summary: Standard JSON strings are converted using Json.parse or eval (' (' +str+ ') '), and non-standard JSON strings can only be attempted with eval (' (' +str+ ') '), plus $ in the jquery framework. Parsejson and Json.parse are the same, and the function of Eval is to execute the string as a JS statement.
2 JS object converted to JSON string
obj = {id: "001", Name: "Sanzhang"}str = json.stringify (obj) "{" id ":" 001 "," Name ":" Sanzhang "}"Summary: JS objects are converted to JSON strings, using json.stringify (obj)
Conversion between a JSON string and a JS object