最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to read json object when i don`t know the name of the key - Stack Overflow

programmeradmin2浏览0评论

in runtime i will have a dynamic dictionary object return to me

e.g var objectFromApi = {"A ":"I am A","B":"I am B","C":"I am C"}

i cannot do objectFromApi ["A"] to get the value, since i won`t able to know the key.

is there a way to print all the key and its value?

is there something like

for(j=0;j<objectFromApi.length;j++)
{
    console.debug(objectFromApi[j].Key +"  " + objectFromApi[j].Value);
}

Thanks

in runtime i will have a dynamic dictionary object return to me

e.g var objectFromApi = {"A ":"I am A","B":"I am B","C":"I am C"}

i cannot do objectFromApi ["A"] to get the value, since i won`t able to know the key.

is there a way to print all the key and its value?

is there something like

for(j=0;j<objectFromApi.length;j++)
{
    console.debug(objectFromApi[j].Key +"  " + objectFromApi[j].Value);
}

Thanks

Share Improve this question asked Jun 27, 2012 at 8:27 jojojojo 13.9k36 gold badges95 silver badges126 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7
for(var name in objectFromApi )
{
    if (objectFromApi.hasOwnProperty(name))
    {

    }
}

http://jsfiddle/V6t6Y/

In ECMA 5 you can also use

var keys = Object.keys(objectFromAPi)

which will return

 ["A ", "B", "C"]

then you can iterate over the array like you normally would

for(var i = 0; i < keys.length; i++){
     // do something with the value
     // objectFromApi[keys[i]] 
}
发布评论

评论列表(0)

  1. 暂无评论