javascript - Get json data from mysql, how does the front end process the conversion and parse the json type
给我你的怀抱
给我你的怀抱 2017-05-18 10:47:53
0
5
714

console.log prompts like this:

data: '{"site_name":"aaaa","site_keywords":"bbbb","site_beian":"闽ICP备8888888888号","site_description":"ccccc","site_statistic":"<a>wsdfadfasdfasdfasdfasdf</a>"}',

Obtaining json data from mysql, this problem occurs. How does the front end handle the conversion and parsing of the json type?

给我你的怀抱
给我你的怀抱

reply all(5)
淡淡烟草味

Convert String to JSON object :

1    var res = JSON.parse(data); 
or
2    var res = data.parseJSON();
or
3    var res = eval('('+ data +')');

某草草

Use JSON.stringify() to convert into a string for subsequent use.

我想大声告诉你

Try to use JSON.parse, eval is not recommended;
Try to add try...catch, the probability of errors when converting JSON to objects is quite high, such as unescaped characters and too many nesting levels;

try{

var myObject = JSON.parse(data);

} catch(e){

console.log(e);

}

phpcn_u1582

The backend sets the Content-Type of the response header to application/json, and the data outputs a string in json format. The frontend automatically obtains the JSON object, which can be processed as an ordinary js object.

左手右手慢动作
var data=[{"site_name":"aaaa","site_keywords":"bbbb","site_beian":"闽ICP备8888888888号","site_description":"ccccc","site_statistic":"<a>wsdfadfasdfasdfasdfasdf</a>"}];

console.log(JSON.stringify(data));
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!