迭代json时未定义

getting undefined while iterating json

本文关键字:未定义 json 迭代      更新时间:2023-09-26

我有一个JSON元素。我需要得到每一种交易类型的交易量。当我尝试

var data = '{"2015:ATM01": {"volume": "2620000","count": "78"},"2015:PIN_PURCHASE01": {"volume": "5162305","count": "101"},"ALL:PIN_PURCHASE01": {"volume": "5162305","count": "101"},"ALL:DIRECT_DEPOSIT01": {"volume": "32752700","count": "50" }}';
    var JSONArray=JSON.parse(data);
    for(var k in JSONArray){
        console.log("key is  is.... "+k);
        console.log("voulume is.... "+k.volume);
    }

我得到未定义的volume。请帮帮我。谢谢。

请检查此提琴:http://jsfiddle.net/1qgvs9o9/

您需要像这样检索音量:

JSONArray[k].volume

k是键(字符串),而不是对象。为了获得您想要的对象,您需要从JSONArray中获取如下值:

var data = '{"2015:ATM01": {"volume": "2620000","count": "78"},"2015:PIN_PURCHASE01": {"volume": "5162305","count": "101"},"ALL:PIN_PURCHASE01": {"volume": "5162305","count": "101"},"ALL:DIRECT_DEPOSIT01": {"volume": "32752700","count": "50" }}';
var JSONArray=JSON.parse(data);
for(var k in JSONArray){
    console.log("key is  is.... "+k);
    console.log("voulume is.... "+JSONArray[k].volume);
}