JSON syntax

a definition

JSON: JavaScript Object Notation (JavaScript Object Notation), which can convert a set of data represented in a JavaScript object into a string, which can then be easily passed between networks or programs, and when needed It was originally a data format supported by various programming languages. For example, when using AJAX, if you need to use an array to pass values, you need to use JSON to convert the array to a string.

two grammar

1. Rules                                                       

In JS language, everything is an object. Therefore, any supported type can be represented by JSON, such as strings, numbers, objects, arrays, etc. But objects and arrays are two special and commonly used types:

  • Objects are represented as key-value pairs
  • Data is separated by commas
  • Curly braces hold the object
  • Square brackets hold arrays

2. JSON key/value pair

JSON key-value pair is a way to save JS objects, and it is similar to the way of writing JS objects. The key name in the key/value pair combination is written in front and wrapped in double quotes "", separated by colon :, and then followed by value:

Example :  {"firstName" : "Json"}

This is easy to understand and is equivalent to this JavaScript statement: {firstName : "Json"}

3. The relationship between JSON and JS

JSON is a string representation of JS objects, which uses text to represent the information of a JS object, which is essentially a string.

Example:

var obj = {a: 'Hello', b: 'World'}; //This is an object, note that the key name can also be wrapped in quotation marks

var json = '{"a": "Hello", "b": "World"}';//This is a JSON string, which is essentially a string.

4. Interchange between JSON and JS objects

To convert from an object to a JSON string, use the JSON.stringify() method:

var json = JSON.stringify({a: 'Hello', b: 'World'}); //结果是 '{"a": "Hello", "b": "World"}'

To convert from JSON to object, use the JSON.parse() method:

var obj = JSON.parse('{"a": "Hello", "b": "World"}'); //结果是 {a: 'Hello', b: 'World'}


Reference link: https://baike.baidu.com/item/JSON/2462549?fr=aladdin#reference-[1]-136475-wrap







Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325919992&siteId=291194637