迭代json时得到undefined

时间:2015-06-08 05:49:23

标签: javascript json

我有一个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);
    }

我将音量定义为未定义。请帮我。感谢。

2 个答案:

答案 0 :(得分:1)

请检查这个小提琴:http://jsfiddle.net/1qgvs9o9/

您需要检索这样的音量:

JSONArray[k].volume

答案 1 :(得分:0)

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);
}