Js parsing Json string

Js parsing Json string

JSON ( JavaScript  Object Notation) is a lightweight data exchange format that adopts a completely language-independent text format and is an ideal data exchange format. At the same time, JSON is a JavaScript native format, which means that processing JSON data in JavaScript does not require any special API or toolkit.

1. An object begins with "{" (left bracket) and ends with "}" (right bracket). Each 'name' is followed by a ':' (colon); 'name/value' pairs are separated by a ',' (comma). The name is enclosed in quotation marks; the value must be enclosed in parentheses if it is a string, but not if it is a numeric value.

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

2. An array is an ordered collection of values. An array starts with "[" (left bracket) and ends with "]" (right bracket). Use "," (comma) to separate values.

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

In order to process JSON data conveniently, JSON provides the json.js package, download address: http://www.json.org/json.js

In the data transmission process, json is transmitted in the form of text, that is, strings, while JS operates on JSON objects. Therefore, the conversion between JSON objects and JSON strings is the key.

E.g:

JSON字符串: var str1 = '{ "name": "cxh", "sex": "man" }';

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

1. Convert JSON string to JSON object

To use the above str1, one must use the following to convert to a JSON object:

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

or

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

or

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

Then, it can be read like this:

alert(obj.name);

alert(obj.sex);

Pay special attention: If obj is originally a JSON object, then it will still be a JSON object after using the eval() function to convert (even if it is converted multiple times), but there will be doubts after using the parseJSON() function to process it (throw a syntax exception).

Second, the JSON object is converted into a JSON string.

For example: var last=obj.toJSONString(); //Convert JSON objects to JSON characters

or

var last=JSON.stringify(obj); //Convert JSON object to JSON character

alert(last);

data set

var str='[{"name":"cxh","sex":"man"},{"name":"cxh1","sex":"man1"}]';

var obj = p.parseJSON ();

alert(obj[0].name);

Note: In the above multiple essentials, except for the eval() function that comes with js, the other multiple essentials come from the json.js package. The new version of JSON modifies the API, injecting both JSON.stringify() and JSON.parse() into Javascript's built-in objects, the former becomes Object.toJSONString(), and the latter becomes String. parseJSON(). If the prompt cannot find the essentials of toJSONString() and parseJSON(), it means that your json package version is too low.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324779610&siteId=291194637