JSON-related knowledge and json-related knowledge
JSON: JavaScript Object Notation (JavaScript Object Notation)
JSON syntax rules
Data in name/value pairs
Data is separated by commas (,).
Brackets save objects
Square brackets Save the Array
JSON has six types of values:
Object, array, String, number, Boolean value, null
A json object is an unordered set that contains "name/value" pairs.
Name: any string
Value: JSON value of any type, including arrays and objects (objects can be embedded)
Note: Double quotation marks must be used for JSON strings (single quotation marks will report errors)
I. Object
Create a literal volume in javascript:
var object = { name:"lily", age:22};
Or:
var object = { "name":"lily", "age":22};
JSON:
{ "name":"lily", "age":22}
2. Array
The JSON array uses the array literal form in javascript.
Extension:
Combining arrays with objects can form more complex data integration.
For example:
[ { "name":"lily", "age":22, "job":"docter" }, { "name":"nicy", "age":21, "job":"teacher" }, { "name":"lily", "age":22, "job":"AE" }]
Iii. parsing and serialization
JSON has a syntax similar to javascript, and can parse the JSON data structure into a useful javascript Object.
1. JSON object
Send and receive JSON data
When reading, writing, sending, and receiving JSON data objects, you must convert them into strings and convert them into JSON data objects. (For reading and writing them in the same way as javascript)
The JSON object has two methods:
① Stringify (): serialize javascript objects into JSON strings
② Parse (): parses the JSON string into a native javascript value.
Instance:
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); //stringvar bookCopy = JSON.parse(jsonText);alert(typeof bookCopy); //object
JSON is used in this example. stringify () serializes A javascript Object book into a JSON string and saves it to jsonText. It directly transmits the JSON string jsonText to JSON. parse () gets the corresponding javascript value.
Note: When a javascript Object is serialized, the final value is an instance property of the valid JSON data type, and any invalid value 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 2: one 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 indent:
The third parameter of the JSON. stringify () method is used to control indentation and blank spaces in the result.
3) toJSON () method
Define the toJSON () method for the object and return its own JSON Data Format
Iv. JSON access value
First: simple array
['Item1', 'item2', 'item3']
Value: Use a digital index to access the embedded value (the first index is 0)
['Item1', 'item2', 'item3']
Var items = ['item1', 'item2', 'item3'];
Alert (items [0]); // item1
Type 2: Use {} to indicate the object and match the Array
{"Key": "value "}
Value: Use a key name to access the embedded value.
Var oExample = {"name": "lily "};
Alert (oExample. name); // lily
Alert (oExample ["name"]); // lily
You can use these two methods to describe many data structures using sub-records (index keys with names or values:
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 all the content of this article. I hope you will like it.