我想要做的就是将这些信息从JSON中提取到我的主JavaScript文件中的console.log中。这是我正在上课的练习练习,明天我想取得好成绩。请帮我?
我试过这个:
//JSON function attempt
var jsonDataPull = function(jsonATMData) {
for (var key in jsonATMData.ATMData) {
for (var otherKey in jsonATMData.ATMData[key]) {
console.log(jsonDataPull.jsonATMData[key][otherKey]);
}
}
};
// actual JSON data I formatted to be able to bring back into my function.
var jsonATMData = {
"ATMData": [
{
"atmLocation": "Main Street",
"atmSumMoney": 3500,
"timeToSteal": 3
},
{
"atmLocation": "14th Street",
"atmSumMoney": 2200,
"timeToSteal": 3
},
{
"atmLocation": "Peachtree Street",
"atmSumMoney": 1700,
"timeToSteal": 4
},
{
"atmLocation": "Peachtree Street",
"atmSumMoney": 300,
"timeToSteal": 1
},
{
"atmLocation": "Monroe Drive",
"atmSumMoney": 660,
"timeToSteal": 1
},
{
"atmLocation": "Ponce",
"atmSumMoney": 5500,
"timeToSteal": 7
}
]
};
答案 0 :(得分:0)
JSONAtmData.ATMData基本上是一个对象数组(图片就像[object,object,object ...]),所以你可以这样做
for ( var i = 0; i < jsonATMData.ATMData.length; i++ ) {
console.log(jsonATMData.ATMData[i].atmLocation);
console.log(jsonATMData.ATMData[i].atmSumMoney);
console.log(jsonATMData.ATMData[i].timeToSteal);
}
答案 1 :(得分:0)
尝试这个:
for(var i in jsonATMData){
for(var n in jsonATMData[i]){
for(var c in jsonATMData[i][n]){
console.log('PROPERTY:'+c+'; VALUE:'+jsonATMData[i][n][c]);
}
}
}
这应该可以在jsonATMData
中显示多个数组。