Home  >  Article  >  Web Front-end  >  js uses eval to parse json examples and precautions to share_javascript skills

js uses eval to parse json examples and precautions to share_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:03:291053browse

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

Copy code The code is as follows:



meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
eval function parses json object






Issues you should pay attention to when using eval() to parse JSON format strings

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);

Complete test code:


Copy code The code is as follows:


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