-1

我有一个如下所示的 JSON。

[
{"Device":"Device1","Links"["NewLink","NewLink2","NewLink3"],"GeographicLocation":"NewLocation"},
{"Device":"Device2","Links":["NewLink"],"GeographicLocation":"NewLocation"}
{"Device":"Device3","Links":["NewLink","NewLink2"],"GeographicLocation":"NewLocation"}
]

我想遍历它并在循环中我想提醒 Links 字段的值。

我怎样才能做到这一点。

4

5 回答 5

0

如果你有一个 json 作为字符串,你可以使用

var json = JSON.parse(jsonString); 

这将返回一个可以迭代的对象数组。

在这里查看更多

于 2013-09-05T10:25:59.553 回答
0
var arr = [
   {"Device":"Device1","Links" ["NewLink","NewLink2","NewLink3"],"GeographicLocation":"NewLocation"},
   {"Device":"Device2","Links":["NewLink"],"GeographicLocation":"NewLocation"}
   {"Device":"Device3","Links":["NewLink","NewLink2"],"GeographicLocation":"NewLocation"}
];

    for(var i=0;i<arr.length;i++){
        var obj = arr[i];
        for(var key in obj){
            var attrName = key;
            var attrValue = obj[key];
        }
    }
于 2013-09-05T10:29:08.520 回答
0
var json = [
    {
        "Device": "Device1",
        "Links": [
            "NewLink",
            "NewLink2",
            "NewLink3"
        ],
        "GeographicLocation": "NewLocation"
    },
    {
        "Device": "Device2",
        "Links": [
            "NewLink"
        ],
        "GeographicLocation": "NewLocation"
    },
    {
        "Device": "Device3",
        "Links": [
            "NewLink",
            "NewLink2"
        ],
        "GeographicLocation": "NewLocation"
    }
];

for(var i=0; i<json.length ; i++)
{
    console.log(json[i].Device);
    console.log(json[i].Links);
    // for links use another loop
    for(var j=0; j<json[i].Links.length ; j++)
    {
        console.log(json[i].Links[j]);
    }
    console.log(json[i].GeographicLocation);
}
于 2013-09-05T10:30:38.333 回答
0

假设你有你的字符串json

var data = JSON.parse(json);
for(var i=0; i<data.length; i++) {
    var links = data[i]['Links'];
    for(var j=0; j<links.length; j++) {
        //append this wherever
        document.write(links[j]);
        //if you're using jQuery, $('body').append(links[j]);
    }
}
于 2013-09-05T10:32:52.363 回答
0
[
{"Device":"Device1","Links":["NewLink","NewLink2","NewLink3"],"GeographicLocation":"NewLocation"},
{"Device":"Device2","Links":["NewLink"],"GeographicLocation":"NewLocation"},
{"Device":"Device3","Links":["NewLink","NewLink2"],"GeographicLocation":"NewLocation"}
]

var json = JSON.parse(jsonString); 

现在它可以工作了

您在 JSON 字符串中错过了“:”“,”

在第一个“链接”附近:},添加这个

于 2013-09-05T10:34:44.780 回答