这是我的 JSON
[
{
"var5":"item-company-1",
"asd2":"item-company-1",
"tie1":"0",
"cxs1":"481.891px",
"xcve2":"130.563px"
},
{
"var5":"item-company-2",
"asd2":"item-company-2",
"tie1":"0",
"cxs1":"481.891px",
"xcve2":"130.563px"
},
{
"var5":"item-company-3",
"asd2":"item-company-3",
"tie1":"0",
"cxs1":"481.891px",
"xcve2":"130.563px"
}
]
如何读取键和值?请记住,我可能不知道 key 。我尝试使用...
var data = JSON.parse(json);
Object.keys(data).forEach(function(prop) {
// `prop` is the property name
// `data[prop]` is the property value
console.log(prop + " = " + data[prop]);
});
但是它只是输出
0 = [object Object]
1 = [object Object]
2 = [object Object]
编辑澄清
在 PHP 中,我得到以下输出,这是我试图从 javascript 实现的输出
0:
var5 => item-company-1
asd2 => item-company-1
tie1 => 0
cxs1 => 481.891px
xcve2 => 130.563px
1:
var5 => item-company-2
asd2 => item-company-2
tie1 => 0
cxs1 => 481.891px
xcve2 => 130.563px
2:
var5 => item-company-3
asd2 => item-company-3
tie1 => 0
cxs1 => 481.891px
xcve2 => 130.563px
最佳答案
你可以试试这个:
var data = [
{
"var5":"item-company-1",
"asd2":"item-company-1",
"tie1":"0",
"cxs1":"481.891px",
"xcve2":"130.563px"
},
{
"var5":"item-company-2",
"asd2":"item-company-2",
"tie1":"0",
"cxs1":"481.891px",
"xcve2":"130.563px"
},
{
"var5":"item-company-3",
"asd2":"item-company-3",
"tie1":"0",
"cxs1":"481.891px",
"xcve2":"130.563px"
}
]
for(var i=0,item;item=data[i++];){
console.log("==========="+i+"=========")
for(var key in item){
console.log(key+":"+item[key])
}
}
关于javascript - 在javascript中获取json对象数组中的JSON键值对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39217676/