我有一个JSON,如下所示。
[
{"Device":"Device1","Links"["NewLink","NewLink2","NewLink3"],"GeographicLocation":"NewLocation"},
{"Device":"Device2","Links":["NewLink"],"GeographicLocation":"NewLocation"}
{"Device":"Device3","Links":["NewLink","NewLink2"],"GeographicLocation":"NewLocation"}
]
我想迭代它并在循环中我想提醒Links字段的值。
我怎样才能做到这一点。
答案 0 :(得分:0)
答案 1 :(得分: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];
}
}
答案 2 :(得分: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);
}
答案 3 :(得分: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]);
}
}
答案 4 :(得分: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字符串中错过了“:”和“,”
靠近第一个“链接”:和},添加此