Home  >  Article  >  Web Front-end  >  Use eval to generate JSON objects under js_javascript skills

Use eval to generate JSON objects under js_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:19:281193browse

For example: var json = eval('(' ret ')');
Suppose we use PHP's encode_json() on the server side to generate the string that needs to be returned
If the generated string is [{"name" :"boke"},{"age":"23"}],
We can directly use eval([{"name":"boke"},{"age":"23"}]) to generate the corresponding JSON object;
If the generated string is {"name":"boke","age":"23"},
We use eval({"name":"boke","age" :"23"}) An error will occur when generating a JSON object
, we need to write eval(({"name":"boke","age":"23"})) like this.
The writing method of eval(( )) is also applicable to other strings generated by the encode_json() function, including the first case.
You can also use a special

Copy code The code is as follows:
function getdata(data){
 return (new Function("return " data) )();
}

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