preface
Most of the time, you need to convert a JSON string to a JSON object, and then process it in a loop, or convert a JSON object to a JSON string as a parameter and pass it to the relevant interface. Next, we will introduce several conversion methods
Assume that the JSON string is u>
Var jsonstr = ‘[{“Id”: 1, “title”: “Zhang San”, “sex”: “male”}, {“Id”: 2, “title”: “Li Si”, “sex”: “male”}]’;
Assume that the JSON object is u>
Var jsonobj = [
{
“Id”: 1,
“title”: “Zhang San”,
“sex”: “male”
},
{
“Id”: 2,
“title”: “Li Si”,
“sex”: “male”
}
]
1、 Convert JSON string to JSON array object
1、eval()
var jsonObj = eval('(' + jsonStr + ')');
2、JSON.parse()
var jsonObj = JSON.parse(jsonStr);
Prev
var jsonObj = JSON.parse(jsonStr);
2、 Convert JSON object to JSON string
JSON.stringify ()
var jsonStr = JSON.stringify(jsonObj);
3、 Loop JSON object
for (var i = 0; i < jsonObj.length; i++) {
var name= jsonObj[i].Title;
}