search

Home  >  Q&A  >  body text

javascript - Get json data from mysql, how does the front end process the conversion and parse the json type

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?

给我你的怀抱给我你的怀抱2748 days ago783

reply all(5)I'll reply

  • 淡淡烟草味

    淡淡烟草味2017-05-18 10:49:53

    Convert String to JSON object :

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

    reply
    0
  • 某草草

    某草草2017-05-18 10:49:53

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

    reply
    0
  • 我想大声告诉你

    我想大声告诉你2017-05-18 10:49:53

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

    }

    reply
    0
  • phpcn_u1582

    phpcn_u15822017-05-18 10:49:53

    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.

    reply
    0
  • ringa_lee

    ringa_lee2017-05-18 10:49:53

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

    reply
    0
  • Cancelreply