Home > Web Front-end > JS Tutorial > body text

Use eval to generate JSON objects under js_javascript skills

WBOY
Release: 2016-05-16 18:19:28
Original
1192 people have browsed it

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template