迭代json对象

时间:2015-07-20 06:43:51

标签: javascript json

我在使用普通的javascript代码迭代json对象时遇到了麻烦。 for循环或每个循环都很棒但是我无法循环它。

//I want output in format like 
{
 applicationGroupId:
 [
  treatmentIds
 ]
}

等;

{
    "879545457291368": [
        879545457291370
    ],
    "879545447124032": [
        879545447124036,
        879545447124034
    ]
}


<script>

var text = '{"treatmentApplicationGroupDataList": [{"applicationGroupId": 879545457291368,"treatments":[{"treatmentId": 879545457291370}]},{"applicationGroupId": 879545447124032,'+
           '"treatments": [{"treatmentId": 879545447124036 },{"treatmentId": 879545447124034}]}]}';

     var myObject = JSON.parse(text);

     for(var i in myObject){

   document.write(myObject.treatmentApplicationGroupDataList[0].applicationGroupId);
   document.write(myObject.treatmentApplicationGroupDataList[0].treatments[0].treatmentId);

   document.write(myObject.treatmentApplicationGroupDataList[1].applicationGroupId);
   document.write(myObject.treatmentApplicationGroupDataList[1].treatments[0].treatmentId);
   ocument.write(myObject.treatmentApplicationGroupDataList[1].treatments[1].treatmentId);

     }

</script>

因为你可以看到我手动打印结果但不是for循环。

3 个答案:

答案 0 :(得分:3)

正如您在示例中尝试的那样,您可以使用for-in方法:

var text = '{"treatmentApplicationGroupDataList": [{"applicationGroupId": 879545457291368,"treatments":[{"treatmentId": 879545457291370}]},{"applicationGroupId": 879545447124032,"treatments": [{"treatmentId": 879545447124036 },{"treatmentId": 879545447124034}]}]}';

var myObject = JSON.parse(text);

// Using the for-in method
for (treatmentListId in myObject.treatmentApplicationGroupDataList) {
    var curTreatmentList = myObject.treatmentApplicationGroupDataList[treatmentListId];

    console.log(curTreatmentList.applicationGroupId+": ");

    for (treatmentId in curTreatmentList.treatments) {
        console.log("=>"+curTreatmentList.treatments[treatmentId].treatmentId);
    }
}    

或者您可以使用.forEach method

myObject.treatmentApplicationGroupDataList.forEach(function(treatmentList){
    console.log(treatmentList.applicationGroupId+": ");
    treatmentList.treatments.forEach(function(treatment){
        console.log("=> "+treatment.treatmentId);
    });
});

此处提供了更多信息:For-each over an array in JavaScript?

答案 1 :(得分:2)

以下是您可以使用的forEach函数的一个很好的解释:https://stackoverflow.com/a/9329476/4693496

鉴于您的以下对象:

{
    "treatmentApplicationGroupDataList": [
        {
            "applicationGroupId": 879545457291368,
            "treatments":
                [
                    {
                        "treatmentId": 879545457291370
                    }
                ]
        },
        {
            "applicationGroupId": 879545447124032,
            "treatments": [
                {
                    "treatmentId": 879545447124036
                },
                {
                    "treatmentId": 879545447124034
                }
            ]
        }
    ]
}

您可以使用以下功能:

myObject.treatmentApplicationGroupDataList.forEach(function(item) {
    document.write(item.applicationGroupId);
    item.treatments.forEach(function(treatment) {
        document.write(treatment.treatmentId);
    });
});

答案 2 :(得分:1)

请参阅以下代码

var text = '{"treatmentApplicationGroupDataList": [{"applicationGroupId": 879545457291368,"treatments":[{"treatmentId": 879545457291370}]},{"applicationGroupId": 879545447124032,'+
           '"treatments": [{"treatmentId": 879545447124036 },{"treatmentId": 879545447124034}]}]}';

var myObject = JSON.parse(text);
var newObj = {};

for(var i in myObject['treatmentApplicationGroupDataList']){
    var appGroupId = myObject['treatmentApplicationGroupDataList'][i]['applicationGroupId'];
    newObj[appGroupId] = [];
    for(var j in myObject['treatmentApplicationGroupDataList'][i]['treatments'])
        newObj[appGroupId].push(myObject['treatmentApplicationGroupDataList'][i]['treatments'][j]['treatmentId'])
}

//newObj will contain the required Json.

<强>释

第一个for循环将遍历treatmentApplicationGroupDataList,然后在该循​​环中将获得此列表的applicationGroupId并使用其key为其创建一个新对象作为applicationGroupIdvaluearray下一个嵌套for循环将遍历该组中的所有treatments以提取treatmentId并将它们推送到上面创建的array for循环中。