如何在json密钥中访问Json密钥 [英] How to access Json key inside a json key

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

问题描述

我对JSON有疑问。我正在使用一个jquery插件,它预期JSON结构如下:

I have a question regarding JSON. I am using a jquery plugin which expected JSON structure as below :

[   { key: "Id" },
            { key: "Username" },
                    { key: "Age" }
        ], 

但是我的JSON看起来像:

but my JSON looks like :

    [{
        "Employee1": 
        {
          "ID": 43036,
          "Name": XYZ,
          "Age": 21
        },
       "Employee2": 
        {
          "ID": 30436,
          "Name": MNP,
          "Age": 23
        }
    }]

现在我不想更改我的代码,是否有任何解决方案以便我可以在不使用Employee的情况下将Id,Name传递给我的插件json。
我需要我的JSON:

Now I don't want to change my code, is there any solution so that I can pass Id , Name to my plugin json without using "Employee". I need my JSON as :

[        
        {
          "ID": 43036,
          "Name": XYZ,
          "Age": 21
        }, 
        {
          "ID": 30436,
          "Name": MNP,
          "Age": 23
        }
    ]

谢谢提前

推荐答案

这样的事情?

var myObj = [{
        "Employee1": 
        {
          "ID": 43036,
          "Name": XYZ,
          "Age": 21
        },
       "Employee2": 
        {
          "ID": 30436,
          "Name": MNP,
          "Age": 23
        }
    }];

var jsonObj = [];
$.each(myObj[0], function(key, val){
     jsonObj.push({ key: val.ID });
     jsonObj.push({ key: val.Name });
     jsonObj.push({ key: val.Age });
}); 

这篇关于如何在json密钥中访问Json密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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