我需要更改从服务器收到的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"
}
}
]
答案 0 :(得分:0)
您可以这样做:
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]
}
答案 1 :(得分:0)
var targetObject = {page: 1, total: 2, rows: []}
yourJSON.each(function (item) {
targetObject.rows.push({cell: item});
});