Home  >  Article  >  Web Front-end  >  What are the methods for JS to operate JSON?

What are the methods for JS to operate JSON?

php中世界最好的语言
php中世界最好的语言Original
2018-04-24 17:20:341521browse

This time I will bring you some methods for JS to operate JSON, and notes for JS to operate JSON. The following is a practical case, let's take a look.

JSON Overview:

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

JSON: JavaScript ObjectNotation (JavaScript Object Notation).

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

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

JSON syntax rules

JSON syntax is a subset of JavaScript object notation syntax.

Data is in name/value pairs

Data is separated by commas

Curly braces save object

Square brackets save array

OK Okay, the above is not the focus of this article. This article mainly summarizes the methods of JS to operate JSON.

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

1. An object starts 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; if the value is a string, it must be enclosed in parentheses, and if it is a numeric value, it is not required. For example:

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). 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"}];

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

During the data transmission process, json is transmitted in the form of text, that is, a string, and JS operates on JSON objects, so the conversion between JSON objects and JSON strings is the key. For example:

JSON string:

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

JSON object:

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

1. JSON Convert string to JSON object

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

//由JSON字符串转换为JSON对象
var obj = eval('(' + str + ')');

or

var obj = str.parseJSON(); //由JSON字符串转换为JSON对象

Or

var obj = JSON.parse(str); //由JSON字符串转换为JSON对象

Then, you can read it like this:

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

Special note: If obj is originally a JSON object, then use the eval() function to convert it (even if Is it multiple conversions) or a JSON object, but there will be problems (throwing a syntax exception) after using the parseJSON() function.

2. You can use toJSONString() or the global method JSON.stringify() to convert the JSON object into a JSON string.

For example:

var last=obj.toJSONString(); //将JSON对象转化为JSON字符

or

var last=JSON.stringify(obj); //将JSON对象转化为JSON字符
alert(last);

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related matters on the php Chinese website article!

Recommended reading:

Adjustment of ajax execution order in jquery

Ajax implements loading waiting effect to improve user experience

The above is the detailed content of What are the methods for JS to operate JSON?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn