0

我需要更改从服务器接收到的 JSON 数据的格式,我很难理解 JSON 属性并且看起来非常令人生畏。任何建议/解决方案将不胜感激。

这是接收到的数据的样子。

[
  {
    "date": "2006-07-01T00:00:00.000Z",
    "date_processed": "2006-06-30T17:45:25.217Z",
    "amount": 98,
    "payment_type": "Debit Order",
    "status": "Collected"
  },
  {
    "date": "2006-08-01T00:00:00.000Z",
    "date_processed": "2006-07-28T17:20:54.000Z",
    "amount": 98,
    "payment_type": "Debit Order",
    "status": "Collected"
  }
]

我需要这个看起来像:

 "page":1,
 "total":2,
 "rows":[
  {
    "cell": {
     "date": "2006-07-01T00:00:00.000Z",
     "date_processed": "2006-06-30T17:45:25.217Z",
     "amount": 98,
     "payment_type": "Debit Order",
     "status": "Collected"
     }
  },
  {
    "cell": {
     "date": "2006-07-01T00:00:00.000Z",
     "date_processed": "2006-06-30T17:45:25.217Z",
     "amount": 98,
     "payment_type": "Debit Order",
     "status": "Collected"
     }
  }
  ]
4

2 回答 2

0

You can simply do:

 var oldJSON = [{}], //your JSON from the server

 newJSON = {
     "page":1,
     "total":oldJSON.length,
     "rows":[]
 };

 for (var i = 0, dataLen = newJSON.total; i < dataLen; i = i + 1) {
      newJSON.rows[i].cell = oldJSON[i]
 }
于 2013-03-25T09:13:29.470 回答
0
    var targetObject = {page: 1, total: 2, rows: []}
    yourJSON.each(function (item) {
       targetObject.rows.push({cell: item});
    });
于 2013-03-25T09:14:48.100 回答