如何删除从api返回的嵌套json对象,如下所示,并通过使用jquery以通用方式取消对象,将其显示为单个字段。这是我的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
] }]"
这是在取消之后应该如何。
[{
"Id": 123,
"Name": "some name",
"createdOnDate": "2013-01-22T17:02:00",
"key1": "this is my key1",
"key2": "this is my key2"
}]
答案 0 :(得分:1)
这是无效的JSON。确保您之前有效。 之后,您可以遍历属性并按照您想要的方式设置它们。
$.each(content[0]['keyValueList'], function (k, value) {
content[0][value['Key']] = value['Value']
});
delete content[0]['keyValueList'];