sarma senagavaram sarma senagavaram - 3 months ago 11
JSON Question

how to access array from stringify object

This is the json object created using JSON.stringify() method.
I am unable to access the data1(array here). how to access and print it.

{"data1":[{"text":"TMS-1","id":"1.0","children":[{"text":"Services","id":"1.0.0","children":[{"text":"child service","id":"1.0.0.0"}]},{"text":"Applications","id":"1.0.1","children":[]},{"text":"Radios","id":"1.0.2","children":[{"text":"radio child","id":"1.0.2.0","children":[{"text":"child 2","id":"1.0.2.0.0"},{"text":"child 2.1","id":"1.0.2.0.1"},{"text":"child 2.2","id":"1.0.2.0.2"}]},{"text":"radio service1","id":"1.0.2.1","children":[{"text":"child 1.2.1","id":"1.0.2.0.0"}]}]},{"text":"Platform","id":"1.0.3","children":[{"text":"platform child","id":"1.0.3.0"}]},{"text":"Faults","id":"1.0.4","children":[{"text":"Fault child","id":"1.0.4.0"}]}]}]}

Answer

Try

var str = JSON.stringify(something); // should be {"data1":[{"text":"TMS-1","id":"1.0","children":[{"text":"Services","id":"1.0.0","children":[{"text":"child service","id":"1.0.0.0"}]},{"text":"Applications","id":"1.0.1","children":[]},{"text":"Radios","id":"1.0.2","children":[{"text":"radio child","id":"1.0.2.0","children":[{"text":"child 2","id":"1.0.2.0.0"},{"text":"child 2.1","id":"1.0.2.0.1"},{"text":"child 2.2","id":"1.0.2.0.2"}]},{"text":"radio service1","id":"1.0.2.1","children":[{"text":"child 1.2.1","id":"1.0.2.0.0"}]}]},{"text":"Platform","id":"1.0.3","children":[{"text":"platform child","id":"1.0.3.0"}]},{"text":"Faults","id":"1.0.4","children":[{"text":"Fault child","id":"1.0.4.0"}]}]}]}
var JsonArray = JSON.parse(str);

read more: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse

Comments