Simple understanding of JSON

1. JSON format

1. What is JSON

        The full name of JSON is "JavaScript Object Notation", which means JavaScript Object Notation, which is a text-based, language-independent, lightweight data interchange format. XML is also a data interchange format, why not choose XML? Because although XML can be used as a cross-platform data exchange format, it is very inconvenient to process XML in JS (short for JavaScript). At the same time, there are more XML tags than data, which increases the traffic generated by the exchange, while JSON does not have any additional tags. JS can be handled as an object, so we prefer JSON to exchange data.

2. Comparison between json format and xml

      Compared with xml, which is also used as a data transmission format, json has the following advantages: 
           - Simple to write, clear structure, and lightweight 
           - It conforms to the native syntax of javascript, and the interpreter engine can process it directly

3. Several regulations for json format:

    • Array or object values ​​can be simple or compound values
    • Simple values ​​include: strings, numbers (must be a decimal identifier), booleans, and Null, of which (NaN, Infinity, -Infinity and undefined are all converted to null)
    • Compound values ​​include arrays or objects conforming to json format
    • A comma cannot be added after the last member of an array or object
    • Strings in arrays or objects must use double quotes, not single quotes
    • Object member names must use double quotes                    

2. JSON object

The json object was added in ES5, which contains two methods: JSON.stringify() and JSON.parse()

1. Uses of JSON.stringify() 
: Convert a value to a string in json format, which can be parsed by JSON.parse(). Note the following:

  • Strings of primitive types are automatically enclosed in double quotes after parsing
  • If a member of the original object is undefined, a function or an xml object, this member is ignored
  • In the original object, if the member of the array is undefined, a function or an xml object, it will be converted to null
  • Regular expressions are converted to empty objects
  • Automatically ignore non-traversable properties in the original object
  • Can accept an array parameter, specifying the property that needs to be converted to a string
  • You can accept a function as a parameter to specify how to convert it into a string. Note that this function recursively processes all keys
  • How the converted object contains a toJSON method, the return value of the method will be used, and the return value will be converted to a string, ignoring other members (which can be used to deal with the case of converting regular expressions)

2. JSON.parse() 
usage: used to convert json formatted strings into objects. If the json string does not conform to the json format, an error will be reported.

 

Guess you like

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