使用JavaScript或jQuery将json的某些部分转换为arry

时间:2015-12-31 14:14:37

标签: javascript jquery arrays json web-services

我遇到了问题,希望使用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 ObjectConverting JSON Object into Javascript array

但是,它没有达到标准,所以我仍在努力。如果有人有解决方案,请告诉我。

1 个答案:

答案 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;