简体   繁体   English

如何遍历JSON数据

[英]How to iterate over JSON data

The following is my JSON data in a div : 以下是我在div JSON数据:

[{"Identifier":"1","Label":"10 Day","Categories":"Standard","UpdatedBy":"Lno","UpdatedAt":"01-02-2013","RefId":"0","ComType":"1","Cs":["944"],"AM":"Error Message","Message":"asdfasdf","Combiner":[{"uniqueID":"1","type":"7","rule":""}]}]

I am accessing it through a JS object: 我正在通过JS对象访问它:

var myArrayVar=JSON.parse(document.getElementById("populateDT").innerHTML);

I want to iterate over this JS object. 我想遍历此JS对象。 The following is my code, but it doesn't access my key/value fields: 以下是我的代码,但无法访问我的键/值字段:

for(var i=0; i<=myArrayVar.length;i++){
    for(var j=0; j<=myArrayVar.Combiner.length; j++){
        var sessionUniqueId= myArrayVar.Combiner[j].uniqueID;
        alert(sessionUniqueId);
        var sessionType=myArrayVar.Combiner[j].type;
        alert(sessionType);
        var sessionRule=myArrayVar.Combiner[j].rule;
        alert(sessionRule);
    }
}

Can anyone suggest a solution? 谁能提出解决方案?

You never use i . 你从不使用i You need it to access the current array element, for example: 您需要它来访问当前数组元素,例如:

for(var j=0; j<=myArrayVar[i].Combiner.length; j++){

myArrayVar is your array, myArrayVar[i] is the i -th element in that array and myArrayVar[i].Combiner is the combiner (array) property of the i -th element. myArrayVar是您的阵列, myArrayVar[i]i该数组中第元件和myArrayVar[i].Combiner是的组合器(阵列)属性i个元素。

You'll make it yourself a lot easier if you give the current element a name as well. 如果还给当前元素起一个名字,您将使自己变得容易得多。 (You probably want to come up with a less generic name such as current though.) (不过,您可能想使用一个不太通用的名称,例如current 。)

for(var i=0; i<myArrayVar.length;i++){
    var current=myArrayVar[i];
    for(var j=0; j<current.Combiner.length; j++){
        var sessionUniqueId=current.Combiner[j].uniqueID;
        alert(sessionUniqueId);
        var sessionType=current.Combiner[j].type;
        alert(sessionType);
        var sessionRule=current.Combiner[j].rule;
        alert(sessionRule);
    }
}

Also, i cannot equal myArrayVar.length as that index is already out of bounds. 另外, i不能等于myArrayVar.length因为该索引已经超出范围。 Your loop condition should have < instead of <= . 您的循环条件应为<而不是<=

You have an array with one element. 您有一个包含一个元素的数组。 That element is in myArrayVar[0] and it is an object. 该元素位于myArrayVar[0] ,并且是一个对象。 To iterate over the object use a for ... in loop. 要遍历对象,请使用for ... in循环。

var myObj = myArrayVar[0];
for(var key in myObj){
    var value = myObj[key];
    console.log(key, value);
}

You should also use console.log for debugging. 您还应该使用console.log进行调试。 It will show you more information about objects than alert can. 它会比警报显示更多有关对象的信息。

for (var i = 0; i < myArrayVar.length; i++) {
    for (var j = 0; j < myArrayVar[i].Combiner.length; j++) {
        var sessionUniqueId = myArrayVar[i].Combiner[j].uniqueID;
        alert(sessionUniqueId);
        var sessionType = myArrayVar[i].Combiner[j].type;
        alert(sessionType);
        var sessionRule = myArrayVar[i].Combiner[j].rule;
        alert(sessionRule);
    }
}

尝试在for循环中使用“ <”代替“ <=”,并使用“ myArrayVar [i] .Combiner”代替“ myArrayVar.Combiner”。

There are a couple of problems I see. 我看到几个问题。 First, your i and j variables go one spot too far. 首先,您的ij变量相距太远。 They should be using < instead of <= . 他们应该使用<而不是<=

Secondly, you're declaring variables inside the loop. 其次,您要在循环内声明变量。 That's fine, but JavaScript isn't block scoped, so you really end up with the same three variables overwriting each other as many times as there are items in the list. 很好,但是JavaScript不受块作用域的限制,因此您实际上最终会遇到相同的三个变量互相覆盖的次数,与列表中的项目一样多。 Your example data only has one item so you probably won't notice the overwriting problem just yet–but once you have multiple items in the list it could be a problem. 您的示例数据只有一个项目,因此您可能还不会注意到覆盖问题-但是一旦列表中有多个项目,就可能是一个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM