如何使用Javascript / Jquery解析多级json

时间:2014-04-27 12:12:46

标签: javascript jquery arrays json

我是JSON的新手,坚持解析多级JSON数组,我想用 javascript或jquery 解析它。从JSON我想得到application idapplication 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"
                    }
                ]
            }
        ]
    }
]

提前致谢:)

2 个答案:

答案 0 :(得分:2)

以下是 Demo

检查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
}

答案 1 :(得分:1)

如果数据最初以JSON(作为字符串)显示,则需要先将其解析为JSON.parse(json)的JavaScript对象。然后,您可以使用对象点表示法访问任何属性。如果您不熟悉JavaScript中的对象,请查看this article