Home  >  Article  >  Web Front-end  >  Summary of JSON related knowledge_json

Summary of JSON related knowledge_json

WBOY
WBOYOriginal
2016-05-16 15:51:371117browse

JSON: JavaScript Object Notation

JSON syntax rules
Data in name/value pairs
Data is separated by commas
Curly brackets save objects
Square brackets save arrays

JSON has 6 types of values:

Object, array, string, number, Boolean value, null

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

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

1. Object

Create literals in javascript:

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

or:

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

JSON:

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

2. Array

JSON array uses the array literal form in javascript
Extension:
Combining arrays and objects can form more complex data combinations
For example:

[
  {
    "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 JavaScript and can parse JSON data structures into useful JavaScript objects

1.JSON object

Send and receive JSON data

When reading, writing, sending and receiving JSON data objects, they need to be converted into strings and can be converted from strings to JSON data objects. (read and write them the same way used for javascript)

JSON object has two methods:
① stringify(): Serialize javascript objects into JSON strings
② parse(): Parse JSON string into 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 a javascript object book into a JSON string, and then save it to jsonText; pass the JSON string jsonText directly to JSON.parse() to get the corresponding javascript Value


Note: When serializing JavaScript objects, the final values ​​are instance properties of valid JSON data types, and any invalid values ​​will be skipped

2. Serialization options

JSON.stringify() can receive two parameters when serializing JavaScript objects
Parameter 1: filter, which can be an array or function
Parameter two: an option indicating whether to retain indentation in the JSON string
1) Filter results
If the filter parameter is an array, the result of JSON.stringify() only contains the attributes listed in the array
For example:

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) String indentation:
The third parameter of the JSON.stringify() method is used to control the indentation and whitespace characters in the result
3) toJSON() method
Define the toJSON() method for the object to return its own JSON data format

4. JSON access value

The first type: simple array
['item1','item2','item3']
Value: Access the embedded value via numeric index (the index of the first item is 0)

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

var oExample = { "name":"lily" };
alert(oExample.name); // lily
alert(oExample["name"]); // lily 
Using these two methods, many data structures can be described in terms of subrecords (with named or numeric index keys):

For example:

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  

The above is the entire content of this article, I hope you all like it.

Statement:
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