JSON.parse error

2

I have a JSON like this:

 {"id":"53530","name":"Bifurcadores <ul class=\'menu\' style=\'display:none\' 
 id=\'ul53530\'> <li onclick=\'pinchar(,\"Todo\")\'>
 <a href=\'#\'>LANZAR PRUEBAS TOTAL <\/a><\/li>",
 "data":{"$color":"#008000","$type":"circle"},
 "children":[{"id":"53443"

 ....etc }

And when doing this in Javascript:

 json=JSON.parse(JSON.stringify(http.responseText));

I get this error:

 jit.js:8795 Uncaught TypeError: Cannot read property 'id' of undefined

Can you help me, please? Thank you very much in advance

    
asked by Mayte 11.10.2016 в 12:45
source

3 answers

1

You have a json object where each object (I imagine) has an array that is children . When you receive the json_encode from php, for example data

$.each(data, function (index) {
      console.log(index.id);
      $.each(this.children, function (children) {
         console.log(children.id);
      });
});

With this you can already manage the html elements that come in your json and add them to your view.

    
answered by 12.11.2016 в 13:30
0

Make sure that your response is an object, so you do not have to do the JSON.parse and JSON.stringify.

var obj = {"id":"53530","name":"Bifurcadores <ul class=\'menu\' style=\'display:none\' id=\'ul53530\'> <li onclick=\'pinchar(,\"Todo\")\'> <a href=\'#\'>LANZAR PRUEBAS TOTAL <\/a><\/li>"};

console.log(obj);
console.log(JSON.parse(JSON.stringify(obj)));

With the data and children part, it must be the same. Verify that your response does not come empty.

    
answered by 11.10.2016 в 22:05
0

Remember that to transmit the Json you must do it as a string.

JSON is a standard (now) format for transmitting objects from one system to another which, being simple, allows any language to build an object from the transmitted text, so if you are going to communicate from a PHP server to a JS script the server should transmit json_encode($obj) and the script should process JSON.parse(objString) ; If it were the other way around, the script should send JSON.stringify(obj) and the server must parse with json_decode($_POST["obj"]) .

    
answered by 07.03.2017 в 17:03