Gaurav Gagan Gaurav Gagan - 5 months ago 84
jQuery Question

How to parse multilevel json using Javascript/Jquery

I am very new to JSON, stuck in parsing multi level JSON array, I want to parse it using javascript or jquery. From the JSON I want to get

application id
,
application description
&
Product description


[
{
"roadMapData": [
{
"applicationDetail": [
{
"applicationDescr": "R25updated-R25updated",
"applicationId": 352
}
]
},
{
"productSubGrupDetail": [
{
"productGroupId": 271,
"productSubGroupDes": "TEST123-TEST1234"
}
]
},
{
"productSubGrupDetail": [
{
"productGroupId": 278,
"productSubGroupDes": "ggg-hhhh"
}
]
}
]
},
{
"roadMapData": [
{
"applicationDetail": [
{
"applicationDescr": "R25updated-R25updated",
"applicationId": 352
}
]
},
{
"productSubGrupDetail": [
{
"productGroupId": 271,
"productSubGroupDes": "TEST123-TEST1234"
}
]
},
{
"productSubGrupDetail": [
{
"productGroupId": 278,
"productSubGroupDes": "ggg-hhhh1"
}
]
}
]
}
]


Thanks in advance :)

Answer

Here is the Demo

Check jQuery.parseJSON

var jsonObj = jQuery.parseJSON(jsonString);
for (i = 0; i < jsonObj.length; i++) {
    var roadMapData = jsonObj[i].roadMapData;
    var applicationDetail = roadMapData[0].applicationDetail; //First Object
    var productSubGrupDetail1 = roadMapData[1].productSubGrupDetail; //Second Object
    var productSubGrupDetail2 = roadMapData[2].productSubGrupDetail; //Third Object
    console.log(applicationDetail[0].applicationDescr); //applicationDetail's First Object
    console.log(productSubGrupDetail1[0].productGroupId); //productSubGrupDetail1's First Object
    console.log(productSubGrupDetail2[0].productSubGroupDes); //productSubGrupDetail2's First Object
}