我如何使用JavaScript迭代这个JSON对象

时间:2014-07-11 03:04:01

标签: javascript json

如何使用带有keys =“1”和“2”的两个实例从此JSON示例中使用javascript提取ColumnValues [i],如下所示。这只是一个示例,我原来的JSON对象有500多个条目,如key =“1”,“2”,“3”,“4”等。当我检索对象的长度时,返回的值等于1因此使用for循环不适用于length属性。感谢任何帮助。

下面的JSON对象

[
 {
    "1": {
        "ChildCount": 0,
        "ColumnIndentLevel": 0,
        "DescendantCount": 0,
        "IndentLevel": 0,
        "IsCategory": false,
        "IsConflict": false,
        "IsDocument": true,
        "IsTotal": false,
        "IsValid": true,
        "NoteID": "3962",
        "SiblingCount": 2,
        "UniversalID": "E8D5D7E88B08CBD686257CD6007470E5",
        "ColumnValues": {
            "0": "03297",
            "1": "Amelia Tang",
            "2": "NBK3456",
            "3": "FHA",
            "4": "2008/10/03 00:00:00 UTC-0000",
            "5": "Withdrawn by SASE Administration",
            "6": "Approver Level 2",
            "7": "Reinstatement"
        }
    },
    "2": {
        "ChildCount": 0,
        "ColumnIndentLevel": 0,
        "DescendantCount": 0,
        "IndentLevel": 0,
        "IsCategory": false,
        "IsConflict": false,
        "IsDocument": true,
        "IsTotal": false,
        "IsValid": true,
        "NoteID": "7972",
        "SiblingCount": 2,
        "UniversalID": "4CEE012B8D60A86A86257CD6007657B0",
        "ColumnValues": {
            "0": "20004484",
            "1": "Anthony Susino",
            "2": "",
            "3": "Conventional",
            "4": "2009/05/11 00:00:00 UTC-0000",
            "5": "Withdrawn by SASE Administration",
            "6": "",
            "7": ""
        }
    }
}]

2 个答案:

答案 0 :(得分:1)

var ColumnValues = /* that object */,
    i = 0;         //this makes sure the object is iterated in an ascending order
while(i++ in ColumnValuesd){         //and ignores irrelevant properties that are
    var item = ColumnValues[i];      //inherited from Object's prototype.
    //do something
}

同样适用于最顶级的密钥。

答案 1 :(得分:1)

使用此:

for (var key in yourObject) { // key will be "1", "2", ...
    var value = yourObject[key];

    // do something
}