JSON in Layman's

Source: Internet
Author: User
Tags tojson javascript array

JSON is a strict subset of JavaScript that uses some of the patterns in JavaScript to represent structured data.


A. JSON Syntax

JSON and XML types are a structured representation of data. Therefore, JSON is not a unique data format for JavaScript, and many other languages can parse and serialize JSON.

The syntax of JSON can represent three types of values:

    • 1. Simple value: You can represent strings, numeric values, Booleans, and Null in JSON. However, JSON does not support special value undefined in JavaScript.
    • 2. Object: As the name implies.
    • 3. Arrays: As the name implies.

Simple values

100. "Lee" is the JSON representation, one is the JSON value, and the other is the JSON string. Boolean values and Null are also valid forms. But the actual use of objects or arrays to be combined.


Object

JavaScript object literal notation:

var box={name: ' Lee ', age:100};

The object notation in JSON requires double quotation marks, and there are no assignment operations and semicolons:

{"Name": "Lee",//use double quotation marks, otherwise the conversion will go wrong "age":  


Array

JavaScript array literal notation:

var box=[100, ' Lee ', true];

The array notation in JSON also has no variable assignments and semicolons:

[+, "Lee", true]

A commonly used complex form is the form of an array-binding object:

[{"title": "A", "num": 1}, {"title": "B", "num": 2}, {"title": "C", "num": 3}]

PS: In general, we can save the JSON structure data into a text file and then load it through the XMLHttpRequest object to get the string of structure data. So, we can simulate this process.

Simulates loading the data of a JSON text file and assigns a value to the variable.

var box= ' [{' name ': ' A ', ' age ': 1},{' name ': ' B ', ' Age ': 2}] ';

PS; This short code simulates the var box= load (' Demo.json ') and the assignment process. Because a text file loaded with load, whatever the content is, it must be a string. So double quotes are added on both sides.

In fact, JSON is more than a normal array of double quotation marks on both sides, the normal array is as follows:

var box=[{name: ' A ', age:1},{name: ' B ', age:2}];


two. Parsing and serialization

If it is a loaded JSON file that we need to use, then we must parse the JSON string into native JavaScript values. Of course, if it is a native JavaScript object or array, it can also be converted to a JSON string.

The eval () function was earlier used for the parsing of JSON strings as native values of JavaScript. However, this approach is not secure and may execute some malicious code.

var box= ' [{' name ': ' A ', ' age ': 1},{' name ': ' B ', ' Age ': 2}] '; alert (box); JSON string var json= eval (box); Use the eval () function to parse alert (JSON); Get JavaScript Native value

ECMASCRIPT5 defines the global object JSON by standardizing the behavior of the parsing JSON. Browsers that support this object are ie8+, firefox3.5+, safari4+, Chrome, and opera10.5+. Unsupported browsers can also simulate execution through an open source library json.js. The JSON object provides two methods, one is to convert the native JavaScript value to a JSON string: Stringify (), and the other is to convert the JSON string to a JavaScript native value: Parse ().

var box= ' [{' name ': ' A ', ' age ': 1},{' name ': ' B ', ' Age ': 2}] '; Pay special attention to the key to use double quotes alert (box); var json= json.parse (box); Not double quotes, Error alert (JSON), var box=[{name: ' A ', age:1},{name: ' B ', age:2}]; JavaScript Native value var json= json.stringify (box); Convert to JSON string alert (JSON); Auto double quotes

The Stringify () method also provides a second parameter during serialization of the JSON. The first argument can be an array, or it can be a function that filters the results. The second parameter indicates whether the indentation is preserved in the JSON string.

var box=[{name: ' A ', age:1,height:177},{name: ' B ', age:2,height:188}]; var json= json.stringify (box,[' name ', ' age '],4); alert (JSON);

PS: If you do not need to retain indentation, then do not fill it, if you do not need to filter the results, but also preserve indentation, the filter results are set to NULL parameters. If the function is used, complex filtering can be done.

var box=[{name: ' A ', age:1,height:177},{name: ' B ', age:2,height:188}]; var json= json.stringify (Box,function (key,value) {switch (key) {case ' name ': Return ' Mr. ' +value; Case ' age ': return value + ' year old '; Default:return value; }},4); alert (JSON);

PS: Retains indentation except for the normal number, or it can be a character.

There is also a way to customize the filtering of some data, using the ToJSON () method, you can specify a certain set of objects to return a value.

var box=[{name: ' A ', age:1,height:177,tojson:function () {return this.name;}},{name: ' B ', Age:2,height:188,tojson: function () {return this.name;}}]; var json= json.stringify (box); alert (JSON);

PS: This shows that serialization also has execution order, first executes the ToJSON () method, if the second filter parameter is applied, the method is executed, and then the serialization process, such as a key-value pair to make a valid JSON string, such as double quotation marks. If indentation is provided, then the indent operation is performed.

Parsing the JSON string method parse () can also accept the second parameter, which can be replaced with the value you want when you restore the JavaScript value.

var box= ' [{' name ': ' A ', ' age ': 1},{' name ': ' B ', ' Age ': 2}] '; var json= json.parse (Box,function (key,value) {if (key = = ' name ') {return ' Mr. ' +value;} else{return value;}}); alert (json[0].name);

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

JSON in Layman's

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.