我遇到了问题,希望使用JavaScript手动将其转换为数组。
{
"code" :"1",
"data" : {
"0" : {"id":"1","score":"44"},
"1" : {"id":"1","score":"44"}
}
}
我想以这种格式进行上述:
{
"code": "1",
"data": [
{"id": "1","score": "44"},
{"id": "2"," score": "45"}
]
}
我尝试使用Remove element from JSON Object和Converting JSON Object into Javascript array。
但是,它没有达到标准,所以我仍在努力。如果有人有解决方案,请告诉我。
答案 0 :(得分:3)
var json = {
"code" :"1",
"data" : {
"0" : {"id":"1","score":"44"},
"1" : {"id":"1","score":"44"}
}
};
var dataArray = Object.keys(json.data).map(function (i) {
return json.data[i];
});
json.data = dataArray;