Home > Web Front-end > JS Tutorial > JSON you don't know much about

JSON you don't know much about

hzc
Release: 2020-06-13 09:43:51
forward
2552 people have browsed it

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\"})")
Copy after login

2. Use Functionnew

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

3. Use JSON.parse

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

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)
Copy after login

Output formatJson as shown below:

JSON you dont 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!

Related labels:
source:juejin.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template