0022. Conversion between JSON and js objects

JSON is a string representation of a JS object. It uses text to represent the information of a JS object, so JSON is essentially a string. Call the corresponding method through JSON, and JSON and JS objects can be converted to each other.

Convert JSON to JS object

Through JSON's parse() method, a JSON can be parsed into a JS object

JSON.parse(json)

For example

var json = '{"name"="张三","age"="36"}';//定义一个JSON 
var obj = JSON.parse(json);//调用parse()将json解析为一个js对象

Convert a JS object to a JSON

Through the stringfy() method of JSON, a JS object can be converted into JSON, and the conversion format is as follows:

JSON.stringify(obj)

For example:

var obj = {name:'张三',age:36'};//定义一个js对象
var json = JSON.stringify(obj); //调用stringify()将一个js对象转换为JSON

Guess you like

Origin blog.csdn.net/Angel_Tears_/article/details/127143508