简体   繁体   English

取消从api返回的JSon对象的嵌套

[英]Unnest a JSon Object returned from api

How can I remove the nested json object returned from an api as shown below and show it as individual fields by unnesting the object using jquery in a generic way. 我如何删除从api返回的嵌套json对象,如下所示,并通过以通用方式使用jquery取消嵌套对象,将其显示为单个字段。 here is my json object 这是我的json对象

 "response": {
"content": 
"[ {    
      "Id": 0,    
     "Name": "Some name",    
     "createdOnDate": "0001-01-01T00:00:00",      
    "keyValueList": 
      [      
           {        
             "Key": "key1",        
            "Value": "Sample Data key 1"      
            },      
           {        
            "Key": "key2",        
           "Value": "sample data key 2   
      ] }]"

this is how it should be after unnesting. 取消嵌套后应该是这样。

[{
    "Id": 123,
        "Name": "some name",
        "createdOnDate": "2013-01-22T17:02:00",
        "key1": "this is my key1",
        "key2": "this is my key2"

}]

This is invalid JSON. 这是无效的JSON。 Make sure you're getting valid before. 确保您之前已经有效。 Afterwards you can iterate through the attributes and set them the way you want. 之后,您可以遍历属性并按所需方式设置它们。

$.each(content[0]['keyValueList'], function (k, value) {
    content[0][value['Key']] = value['Value']
});

delete content[0]['keyValueList'];

http://jsfiddle.net/TjQzv/3/ http://jsfiddle.net/TjQzv/3/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM