如何在javascript中检索键值 [英] How to retrieve key values in javascript

查看:57
本文介绍了如何在javascript中检索键值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的json数据格式如下:

My json data format is like below:

"INFO" : {
    "DETAILS" : {
        "EMP" : {
             "amount": " 12185",
             "job": "GAPA",
             "month": "JANUARY",
             "year": "2010"
         }
     }
},

我在上面的json中获取了键的值。获得关键值后,我想得到地图(即)信息。

I am getting the values of keys in the above json. After getting the key values I want to get the map (i.e) INFO.

现在,我想为提交等按钮设置id或属性值为INFO。

Now, I want to set the id or a attribute value as "INFO" for a button like submit.

我该怎么办?

推荐答案

目前还不完全清楚你在问什么,但如果你想循环通过这些项目,你可以做到这一点。

It's not entirely clear what you're asking, but if you want to loop through the items, you can do this.

var json = '{"INFO" : {"DETAILS" : {"EMP" : {"amount": " 12185","job": "GAPA","month": "JANUARY","year": "2010"}}}}',
  i;

json = JSON.parse(json);

for (i in json.INFO.DETAILS.EMP) {

  // always check for own properties when looping through an object
  if (json.INFO.DETAILS.EMP.hasOwnProperty(i)) {
    console.log('key: %s, value: %s', i, json.INFO.DETAILS.EMP[i]);
  }
}

这篇关于如何在javascript中检索键值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆