Home >Web Front-end >JS Tutorial >Convert js string to JSON_basic knowledge

Convert js string to JSON_basic knowledge

WBOY
WBOYOriginal
2016-05-16 17:16:561265browse

Example 1
In JS, convert the json format string into a JSON object, the key code

Copy code The code is as follows:

json = eval('(' str ')');

The method is as follows:
Copy code The code is as follows:





JSON application





Example 2 Using new function form
Copy code The code is as follows:

function strToJson(str){
var json = (new Function(" return " str))();
return json;
}

Example 3 Using related json2 plug-in instance
Copy code The code is as follows:



  var a = ' { "name": "tom", "sex": "Male", "age": "24" }'; // String used to form json;
alert("String used to form json :【" a "】");
var b = JSON2.parse(a); //Convert string a to json format b
var c = JSON2.stringify(a); //Convert json Convert format b to string c
alert("Converted string: [" c "]");
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