The example in this article describes the method of JavaScript parsing JSON format data. Share it with everyone for your reference, as follows:
1. Use the eval() function provided by JavaScript
function JsonText1() { var strJSON = "{'Name':'Kevin','Age':'23'}"; //得到的JSON var obj = eval("(" + strJSON + ")"); //转换后的JSON对象 alert(obj.Name); }
2. Use JSON object
① Use stringify of JSON object () function, convert the object into JSON
Syntax: var str = JSON.stringify(data);
② Use the parse() function of the JSON object to convert JSON into the object
Syntax: var data = JSON.parse(str);
var jsonStr = ""; //使用JSON对象的stringify()函数,将对象转换成JSON function JsonText2() { var data = new Object; data.Name = "Kevin"; data.Age = 23; jsonStr = JSON.stringify(data); alert(jsonStr); } //使用JSON对象的parse()函数,将JSON转换成对象 function JsonText3() { var data = JSON.parse(jsonStr); alert(data.Name); }
I hope this article will be helpful to everyone in JavaScript programming.
For more examples of JavaScript methods to parse JSON format data and related articles, please pay attention to the PHP Chinese website!