Home >Web Front-end >JS Tutorial >js uses eval to parse json examples and precautions to share_javascript skills
JSON (JavaScript Object Notation) is a simple data format that is more lightweight than xml. JSON is a native JavaScript format, which means that processing JSON data in JavaScript does not require any special API or toolkit.
The rules of JSON are simple: an object is an unordered collection of "name/value pairs". An object starts with "{" (left bracket) and ends with "}" (right bracket). Each "name" is followed by a ":" (colon); "'name/value' pairs" are separated by "," (comma)
Let’s look at an analysis example first
Issues you should pay attention to when using eval() to parse JSON format strings
Use the built-in eval function of javascript to convert the json format string into JS object, you need to wrap the string with a pair of "()" first.
For example:
var strTest="{id: "cnlei", url: "http://www.jb51 .net"}"; Convert to JS object
Correct writing:
var objTEST=eval("(" strTEST ")");
Incorrect writing:
var objTEST=eval(strTEST);