preface

This is a very low probability of being asked in an interview, but it’s still possible.

Why JSON is popular

  1. Has a syntax similar to JS
  2. JSON data structures can be parsed into JS objects
  3. Compared to XML data structures, extracting data is simpler

JSON syntax

Simple values

  • String = > “123”
  • Value = > 123
  • Boolean value = > true
  • null

object

{” name “:” helin “, “age” : 12}

An array of

[ {“name”: “iskeeping”,”age”: 12} ]

Note: The key of JSON must be quoted in double quotes, and the value must be quoted in double quotes if it is a string.

JSON parsing

  1. The use of eval
eval("({\"name\":\"iskeeping\"})")
Copy the code
  1. Use the Function
new Function(""."return ({\"name\":\"iskeeping\"})") ()Copy the code
  1. Using JSON. The parse
JSON.parse("{\"name\":\"iskeeping\"}")
Copy the code

JSON serialization

Let json = simple value/object/array

Json.stringify (JSON,null/[]/function(key,value){}, indent space length/string)

Ex. :

let formateJson = JSON.stringify({ name: "iskeeping", age: 12 }, function (key, value) {
    switch (key) {
        case "name": return "iskeepingxx";
        default: return value;
    }
}, 8)
cosnole.log(formateJson)
Copy the code

Output formateJson as shown below:

JSON native support

Native JSON objects are supported by the following browsers.

IE8+, Firefox3.5+, Safari 4+, Opera 10.5 and chrome

Thanks for reading!

Need to add wechat communication, can leave a message!