用数组解析json对象

时间:2013-09-07 13:43:53

标签: javascript json

我有一个这种形式的json:

 var test = { "name1" : [ "aaa", "bbb"], "name2" : [ "ccc", "ddd" ] };

我试图像

那样迭代这个
for (var names in test )
{
    for ( var element in names )
    {
        // problem we got the chars in the "names1", etc
    }
}

我到底错过了什么?

1 个答案:

答案 0 :(得分:1)

要迭代test对象,你需要这样的东西:

for(var key in test) //iterate object keys
{
    var elements = test[key];
    for(var i = 0; i < elements.length; i++) //iterate array indexes
    {
        var element = elements[i];
    }
}