Conversion between a JSON string and a JS object

Source: Internet
Author: User

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
  1. ID: "001"
  2. Name: "Sanzhang"
  3. __proto__: Object
]
Array string "[{\" id\ ": \" 001\ ", \" name\ ": \" sanzhang\ "}]" corresponding to the JSON format [{"id": "001", "Name": "Sanzhang"}] [Object
  1. ID: "001"
  2. Name: "Sanzhang"
  3. __proto__: Object
]
[Object
  1. ID: "001"
  2. Name: "Sanzhang"
  3. __proto__: Object
]
[Object
  1. ID: "001"
  2. Name: "Sanzhang"
  3. __proto__: Object
]
[Array[1]
  1. 0: Object
    1. ID: "001"
    2. Name: "Sanzhang"
    3. __proto__: Object
  2. Length: 1
  3. __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
  1. ID: "001"
  2. Name: "Sanzhang"
  3. __proto__: Object
, Object
  1. ID: "{" ID1 ":" 1 "," Id2 ":" 2 "}"
  2. __proto__: Object
]
[Object
  1. ID: "001"
  2. Name: "Sanzhang"
  3. __proto__: Object
, Object
  1. ID: "{" ID1 ":" 1 "," Id2 ":" 2 "}"
  2. __proto__: Object
]
[object
  1. id:  "001"
  2. name: " Sanzhang "
  3. __proto__: object
, Object
  1. Id: " {"ID1": "1", "Id2": "2" } "
  2. __proto__: object
]
[Array[2]
  1. 0: Object
    1. ID: "001"
    2. Name: "Sanzhang"
    3. __proto__: Object
  2. 1: Object
    1. ID: "{" ID1 ":" 1 "," Id2 ":" 2 "}"
    2. __proto__: Object
  3. Length: 2
  4. __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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.