1

我想删除嵌套的 json 对象并将其单独显示为键值对。这是我的 json 对象

[
  {
    "fileId": 123,
    "fileName": "test file.zip",
    "createdOnDate": "2013-03-22T17:02:00",    
    "valueList": {
      "text_box": "Testing textbox",
      "text_area": "Lorem ipsum dolor sit"
    }
  }
]

如何格式化它,使对象看起来像

[
  {
    "fileId": 123,
    "fileName": "test file.zip",
    "createdOnDate": "2013-03-22T17:02:00",    
    "text_box": "Testing textbox",
      "text_area": "Lorem ipsum dolor sit"

  }
]

使用jQuery?

4

2 回答 2

0

试试这个:

theArray.forEach(function(item) {
    item.text_box = item.valueList.text_box;
    item.text_area = item.valueList.text_area;
    delete item.valueList;
});
于 2013-03-28T18:43:43.773 回答
0

这是一个简单但并非包罗万象的解决方案。

function unseat(obj, prop) {
    for(var k in obj[prop])
        obj[k] = obj[prop][k];
    delete obj[prop];
    return obj;
}
var testObj =   {
    "fileId": 123,
    "fileName": "test file.zip",
    "createdOnDate": "2013-03-22T17:02:00",    
    "valueList": {
      "text_box": "Testing textbox",
      "text_area": "Lorem ipsum dolor sit"
    }
  };
testObj = unseat(testObj, "valueList");
for(var k in testObj) 
    console.log(k + " " + testObj[k]);
于 2013-03-28T18:47:26.307 回答