Home  >  Article  >  Web Front-end  >  JSON you don’t know much about

JSON you don’t know much about

hzc
hzcforward
2020-06-13 09:43:512406browse

Preface


Although the probability of being asked about this knowledge point in the interview is very low, it may still be asked.

The reason why JSON is popular


  1. Has a syntax similar to js

  2. You can JSON data structure is parsed into js objects

  3. Compared with XML data structure, extracting data is simpler

JSON syntax


Simple value

  1. String=>“123”

  2. Value= >123

  3. Boolean=>true

  4. ##null

Object

{"name": "helin","age": 12}

Array

[ {"name": " iskeeping","age": 12} ]

Note: The JSON key must use double quotes, and if the value is a string, it must also use double quotes.

JSON parsing method

1. Use eval

eval("({\"name\":\"iskeeping\"})")

2. Use Functionnew

Function("", "return ({\"name\":\"iskeeping\"})")()

3. Use JSON.parse

JSON.parse("{\"name\":\"iskeeping\"}")

JSON serialization


let json = simple value/object/array

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

Example:

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

Output formatJson as shown below:

JSON you don’t know much about

##JSON Native support

Native JSON objects are supported by the following browsers.

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

Thank you for reading!

If you need to communicate on WeChat, you can leave a message!

Recommended tutorial: "

JS Tutorial

"

The above is the detailed content of JSON you don’t know much about. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.cn. If there is any infringement, please contact admin@php.cn delete