JS Operation JSON Method Summarizes of Recommendations

  • 2021-06-28 10:43:23
  • OfStack

JSON Overview:

JSON (JavaScript Object Notation) is a lightweight data exchange format. It uses a language-independent text format and is an ideal data exchange format.At the same time, JSON is JavaScript's native format, which means that no special API or toolkit is required to process JSON data in JavaScript.

JSON: JavaScript object representation (JavaScript Object Notation).

JSON is the syntax for storing and exchanging text information.Similar to XML.

JSON is smaller, faster, and easier to parse than XML.

JSON Grammar Rules

JSON grammar is a subset of JavaScript object representation French.

Data in name/value pairs

Data is separated by commas

Curly brackets to save objects

Square brackets hold arrays

Okay, the above is not the focus of this article, this article is mainly to summarize the JS operation method of JSON.

In JSON, there are two structures: objects and arrays.

1. An object starts with'{'(left parenthesis) and ends with'}' (right parenthesis).Each "name" is followed by a ":" (colon); "'Use "," (comma) to separate name/value pairs.Names are enclosed in quotation marks;Values must be parenthesized if they are strings, and numeric types do not.For example:


var o={"xlid":"cxh","xldigitid":123456,"topscore":2000,"topplaytime":"2009-08-20"} ;  

2. Arrays are ordered collections of values (value).An array begins with'['(left middle bracket) and ends with']' (right middle bracket).Values are separated by ","(comma).

For example:


var jsonranklist=[{"xlid":"cxh","xldigitid":123456,"topscore":2000,"topplaytime":"2009-08-20"},{"xlid":"zd","xldigitid":123456,"topscore":1500,"topplaytime":"2009-11-20"}];

To facilitate the processing of JSON data, JSON provides the json.js package for download at http://www.json.org/json.js

During data transfer, json is passed as text, that is, a string, while JS operates on an JSON object, so the conversion between the JSON object and the JSON string is key.For example:

JSON string:


var str1 = '{ "name": "cxh", "sex": "man" }'; 

JSON object:


var str2 = { "name": "cxh", "sex": "man" }; 

1. JSON string to JSON object

To use str1 above, you must first convert it to an JSON object using the following methods:


// from JSON String to JSON object 
var obj = eval('(' + str + ')');

perhaps


var obj = str.parseJSON(); // from JSON String to JSON object  

perhaps


var obj = JSON.parse(str); // from JSON String to JSON object 

Then you can read as follows:


Alert(obj.name);
Alert(obj.sex);

Special note: If obj is an JSON object, it will be a problem (throwing a syntax exception) if eval() function is used to convert it (even if it is converted more than once) or an JSON object.

2. The JSON object can be converted to an JSON string using toJSONString () or the global method JSON.stringify ().

For example:


var last=obj.toJSONString(); // take JSON Object conversion JSON character 

perhaps


var last=JSON.stringify(obj); // take JSON Object conversion JSON character 
alert(last);

Friendship on this site reminds you of the following issues:

Among the above methods, except that the eval() function comes with js, the others are from the json.js package.The new version of JSON modifies API by injecting both methods, JSON.stringify(), and JSON.parse(), into the built-in object of Javascript, which becomes Object.toJSONString(), and String.parseJSON().If you are prompted that the toJSONString () and parseJSON () methods cannot be found, your json package version is too low.


Related articles: