How to parse json in javascript having dynamic key value pair? -
this question has answer here:
i want parse json string in javascript. response like
var response = '{"1":10,"2":10}';
how can each key , value json ?
i doing -
var obj = $.parsejson(responsedata); console.log(obj.count);
but getting undefined
obj.count
.
to access each key-value pair of object, can use object.keys
obtain array of keys can use them access value [ ] operator. please see sample code below:
object.keys(obj).foreach(function(key){ var value = obj[key]; console.log(key + ':' + value); });
output:
1 : 10
2 : 20
objects.keys
returns array of keys in object. in case, ['1','2']
. can therefore use .length
obtain number of keys.
object.keys(obj).length;
Comments
Post a Comment