SoFunction
Updated on 2025-02-28

A detailed summary of the essentials of JS operation JSON

JSON (JavaScript Object Notation) is a lightweight data exchange format that uses a completely language-independent text format and is an ideal data exchange format. Meanwhile, JSON is a JavaScript native format, which means that no special API or toolkit is required to process JSON data in JavaScript.
This article mainly summarizes the key points of JS operating JSON.
In JSON, there are two structures: objects and arrays.
1. An object starts with "{" (open bracket) and ends with "}" (close bracket). Each "name" is followed by a ":" (colon); "'name/value' pair" is separated by "," (comma). The name is enclosed in quotes; if the value is a string, it must be in brackets, and if the numerical type is not required. For example:
Copy the codeThe code is as follows:

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

2. Arrays are ordered collections of values. An array starts with "[" (left middle bracket) and ends with "]" (right middle bracket). Values ​​are separated by "," (comma).
For example:
Copy the codeThe code is as follows:

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 facilitate processing of JSON data, JSON provides a package, download address:/
In the data transmission process, json is passed in the form of text, that is, strings, while JS operates on JSON objects, so the mutual conversion between JSON objects and JSON strings is the key. For example:
JSON string:
Copy the codeThe code is as follows:

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

JSON object:
Copy the codeThe code is as follows:

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

1. Convert JSON string to JSON object
To use str1 above, you must use the following to lead to convert it into a JSON object:
Copy the codeThe code is as follows:

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

Then, you can read it like this:
Copy the codeThe code is as follows:

Alert();
Alert();

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

2. You can use toJSONString()Or global key () converts JSON objects into JSON strings.
For example:
Copy the codeThe code is as follows:

var last=(); //Convert JSON object to JSON characters
or
var last=(obj); //Convert JSON object to JSON characters
alert(last);

Pay attention:
Among the above points, except for the eval() function that comes with js, the other points come from the package. The new version of JSON has modified the API and injected both () and () into the built-in objects of Javascript. The former becomes () and the latter becomes (). If the prompt cannot find the key points toJSONString() and parseJSON(), it means that your json package version is too low.