getting undefined while iterating json

115 views Asked by At

I have a JSON element. I need to get the volume of every transaction type . When I try

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

I get volume as undefined . Please help me. Thanks.

2

There are 2 answers

0
Jyoti Puri On BEST ANSWER

Please check this fiddle: http://jsfiddle.net/1qgvs9o9/

You need to retrieve volume like this :

JSONArray[k].volume
0
Literphor On

k is a key(string), not an object. In order to get the object you want you need to get the value from JSONArray like so:

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