Ajax响应转换

时间:2015-04-07 18:33:34

标签: javascript jquery json

我有一个由ajax返回的数组:

console.log(res);
["07Apr|1", "06Apr|3", "05Apr|12", "04Apr|11", "03Apr|0", "02Apr|0", "01Apr|6", "31Mar|0", "30Mar|7", "29Mar|16", "28Mar|5", "27Mar|5", "26Mar|12", "25Mar|9", "24Mar|4", "23Mar|10", "22Mar|16", "21Mar|2", "20Mar|19", "19Mar|22", "18Mar|10", "17Mar|11", "16Mar|10", "15Mar|19", "14Mar|0", "13Mar|4", "12Mar|14", "11Mar|5", "10Mar|26", "09Mar|7", "08Mar|5"] 

我使用JSON.stringify转换此数组:

(变量“res”是我的ajax的响应 - 如上所示)

            var obj = [];
            var daysBack = 30;
            var objItem = {};
            for(var x = 0; x <= daysBack; x++){
                var currObj = res[x];
                var objCombo = currObj.split("|");
                var objItem = "{date: '"+objCombo[0]+"', downloads: '"+objCombo[1]+"'}";
                objItem = JSON.stringify(eval("(" + objItem + ")"));
                obj.push(objItem);
            }

当我将“obj”转储到控制台时,我得到:

console.log(obj); 
["{"date":"07Apr","downloads":"1"}", "{"date":"06Apr","downloads":"3"}", "{"date":"05Apr","downloads":"12"}", "{"date":"04Apr","downloads":"11"}", "{"date":"03Apr","downloads":"0"}", "{"date":"02Apr","downloads":"0"}", "{"date":"01Apr","downloads":"6"}", "{"date":"31Mar","downloads":"0"}", "{"date":"30Mar","downloads":"7"}", "{"date":"29Mar","downloads":"16"}", "{"date":"28Mar","downloads":"5"}", "{"date":"27Mar","downloads":"5"}", "{"date":"26Mar","downloads":"12"}", "{"date":"25Mar","downloads":"9"}", "{"date":"24Mar","downloads":"4"}", "{"date":"23Mar","downloads":"10"}", "{"date":"22Mar","downloads":"16"}", "{"date":"21Mar","downloads":"2"}", "{"date":"20Mar","downloads":"19"}", "{"date":"19Mar","downloads":"22"}", "{"date":"18Mar","downloads":"10"}", "{"date":"17Mar","downloads":"11"}", "{"date":"16Mar","downloads":"10"}", "{"date":"15Mar","downloads":"19"}", "{"date":"14Mar","downloads":"0"}", "{"date":"13Mar","downloads":"4"}", "{"date":"12Mar","downloads":"14"}", "{"date":"11Mar","downloads":"5"}", "{"date":"10Mar","downloads":"26"}", "{"date":"09Mar","downloads":"7"}", "{"date":"08Mar","downloads":"5"}"]

现在,我想从这个改变上面的数组格式:

["{"date":"07Apr","downloads":"1"}", "{"date":"06Apr","downloads":"3"}", "{"date":"05Apr","downloads":"12"}", "{"date":"04Apr","downloads":"11"}", ....]

到那一个:

[{"date":"07Apr","downloads":"1"}, {"date":"06Apr","downloads":"3"}, {"date":"05Apr","downloads":"12"}, {"date":"04Apr","downloads":"11"}, ....]

我的意思是消除用花括号括起来的双引号。

任何想法都会受到赞赏......

2 个答案:

答案 0 :(得分:2)

在整个结构准备就绪时将其整齐,而不是分段。

不要使用eval,而是使用JSON.parse()。

        var obj = [];
        var daysBack = 30;
        var objItem = {};
        for(var x = 0; x <= daysBack; x++){
            var currObj = res[x];
            var objCombo = currObj.split("|");
            var objItem = {date: objCombo[0], downloads: objCombo[1]};
            //objItem = JSON.stringify(eval("(" + objItem + ")"));
            obj.push(objItem);
        }
        obj = JSON.stringify(obj);

答案 1 :(得分:2)

您可以解析从ajax响应中收到的数据以创建数组。

var populateArray = function (ajaxResponse) {
    var newArray = [];
    ajaxResponse.forEach(function (item, index) {
        var props = item.split('|');
        var obj = {
            date: props[0],
            downloads: props[1]
        };
        newArray.push(obj)
    });

    return newArray;
}

要演示,请尝试

console.log(populateArray(["07Apr|1", "06Apr|3", "05Apr|12", "04Apr|11", "03Apr|0", "02Apr|0", "01Apr|6", "31Mar|0", "30Mar|7", "29Mar|16", "28Mar|5", "27Mar|5", "26Mar|12", "25Mar|9", "24Mar|4", "23Mar|10", "22Mar|16", "21Mar|2", "20Mar|19", "19Mar|22", "18Mar|10", "17Mar|11", "16Mar|10", "15Mar|19", "14Mar|0", "13Mar|4", "12Mar|14", "11Mar|5", "10Mar|26", "09Mar|7", "08Mar|5"]));