JSON knowledge summary

  • 2020-07-21 06:42:59
  • OfStack

JSON: JavaScript object representation (JavaScript Object Notation)

JSON grammar rules
The data is in the name/value pair
Data is separated by commas
Curly braces hold objects
Square brackets hold arrays

JSON has six types of values:

Object, array, string, number, Boolean, null

The JSON object is an unordered collection of name/value pairs

Name: Arbitrary string
Value: JSON value of any type, including arrays and objects (objects can be embedded in objects)
Note: The JSON string must be in double quotes (single quotes will give an error)

Object 1.

Create literals in javascript:


var object = {
  name:"lily",
  age:22
};

Or:


var object = {
  "name":"lily",
  "age":22
}; 

JSON:


{
  "name":"lily",
  "age":22
} 

An array of 2.

The JSON array takes the literal form of the array in javascript
Extension:
Arrays and objects can be combined to form more complex data combinations
Such as:


[
  {
    "name":"lily",
    "age":22,
    "job":"docter"
  },
  {
    "name":"nicy",
    "age":21,
    "job":"teacher"
  },
  {
    "name":"lily",
    "age":22,
    "job":"AE"
  }
]  

3. Parsing and serialization

JSON has a syntax similar to that of javascript to parse JSON data structures into useful javascript objects

1. JSON object

Send and receive JSON data

When an JSON data object is read, written, sent, and received, it needs to be converted to a string and can be converted from a string to an JSON data object. (Used for javascript to read and write them the same way)

There are two methods for an JSON object:
stringify(): Serializes the javascript object to the JSON string
parse(): Parses the JSON string to the native javascript value

Example:


var book = {
  title:"professional JavaScript",
  authors:[
    "lily"
  ],
  edition:3,
  year:2011
};
var jsonText = JSON.stringify(book);
alert(jsonText);   //{"title":"professional JavaScript","authors":["lily"],"edition":3,"year":2011}
alert(typeof jsonText);   //string
var bookCopy = JSON.parse(jsonText);
alert(typeof bookCopy);   //object 

In this example, JSON. stringify() is used to serialize an javascript object book to an JSON string and then save it to jsonText; The JSON string jsonText is passed directly to JSON. parse() to obtain the corresponding javascript value


Note: When serializing an javascript object, the final value is an instance property of the valid JSON data type, and any invalid value is skipped

2. Serialization options

JSON. stringify() can take two arguments when serializing an javascript object
Parameter 1: Filter, which can be an array or function
The 2:1 option indicates whether to retain indenting in the JSON string
1) Filtering results
If the filter parameter is an array, then the result of JSON.stringify () contains only the properties listed in the array
Such as:


var book = {
  "title":"professional JavaScript",
  "authors":[
    "lily"
  ],
  edition:3,
  year:2011
}; 
var jsonText = JSON.stringify(book,["title","edition"]);
alert(jsonText); //{"title":"professional JavaScript","edition":3}
alert(typeof jsonText); // string 

2) Indenting:
The third parameter of the JSON.stringify () method is used to control the indentation and whitespace in the result
3) toJSON() method
Define an object's toJSON() method that returns its own JSON data format

4. JSON access value

Type 1: Simple arrays
['item1','item2','item3']
Value: Access the embedded value by a digital index (the index for item 1 is 0)

['item1','item2','item3']
var items = ['item1','item2','item3'];
alert (items [0]); / / item1
Type 2: Use {} to represent objects and coincidence arrays
{ "key":"value" }
Value: Access the embedded value by the key name

var oExample = { "name":"lily" };
alert(oExample.name); // lily
alert (oExample [" name] "); / / lily
Using these two approaches, you can describe many data structures with child records (named or numeric index keys) :

Such as:


var oNovelist = {
  "firstName":"lily",
  "lastName":"russ",
  "novels":
      [
        {
          "title":"and choas died",
          "year":"1970"
        },
        {
          "title":"the famale man",
          "year":"1976"
        }
      ]
}; 
var msg = oNovelist.firstName+" "+oNovelist.lastName+"'s"+" "+oNovelist.novels[0].title+" "+"was published in"+oNovelist.novels[0].year;
alert(msg);   // lily russ's and choas died was published in1970  

This is the end of this article, I hope you enjoy it.


Related articles: