0

私はjqueryでajaxリクエストを作成し、その代わりに応答を取得していますが、配列としては取得していません。

{"ErrorCode":0,"SeriesSocialStats":{"8970471":{"faves":1,"friendFaves":0,"friendLikes":0,"likes":1,"myFaves":1,"myLikes":0,"seriesId":"8970471"}}}
{"ErrorCode":0,"SeriesSocialStats":{"184072":{"faves":2,"friendFaves":0,"friendLikes":0,"likes":2,"myFaves":1,"myLikes":0,"seriesId":"184072"}}}

上記の2つの応答をマージして、次のような配列を作成したいと思います。

{"faves":1,"friendFaves":0,"friendLikes":0,"likes":1,"myFaves":1,"myLikes":0,"seriesId":"8970471"},{"faves":2,"friendFaves":0,"friendLikes":0,"likes":2,"myFaves":1,"myLikes":0,"seriesId":"184072"}

それを行う方法を提案してください。私はそれを配列に入れてローカルに保存したいのですが、config varaibleget:[]にあり、のようにアクセスできますconfig.get[data["seriesId"]]

4

3 に答える 3

0

あなたができる

var obj1 = {"ErrorCode":0,"SeriesSocialStats":{"8970471":{"faves":1,"friendFaves":0,"friendLikes":0,"likes":1,"myFaves":1,"myLikes":0,"seriesId":"8970471"}}};

var obj2 = {"ErrorCode":0,"SeriesSocialStats":{"184072":{"faves":2,"friendFaves":0,"friendLikes":0,"likes":2,"myFaves":1,"myLikes":0,"seriesId":"184072"}}};

var arr = [];

arr.push(ob1.SeriesSocialStats);
arr.push(ob2.SeriesSocialStats);
于 2012-04-06T12:51:27.280 に答える
0

オブジェクトから配列への二重処理を行う代わりに、Josephが述べたように、サーバーの応答を配列構造に変換する最良の方法。

于 2012-04-06T13:05:03.467 に答える
0

応答をオブジェクトの配列に変換する必要があります。

var response = [
    {"ErrorCode":0,...},
    {"ErrorCode":0,...},
    {"ErrorCode":0,...},
]

実際には:

jsonResponse = [
    {"ErrorCode":0,"SeriesSocialStats":{"8970471":{"faves":1,"friendFaves":0,"friendLikes":0,"likes":1,"myFaves":1,"myLikes":0,"seriesId":"8970471"}}},
    {"ErrorCode":0,"SeriesSocialStats":{"184072":{"faves":2,"friendFaves":0,"friendLikes":0,"likes":2,"myFaves":1,"myLikes":0,"seriesId":"184072"}}}
]

次にループします:

var newArray = []
for(var i=0;i<jsonResponse.length;i++){             //loop through items
    var stats = jsonResponse[i].SeriesSocialStats;
    for(key in stats){                              //loop through "SeriesSocialStats" numbers
        newArray.push(stats[key]);
    }
}

したがって、次のようになります。

newArray = [
    {"faves":1,"friendFaves":0,"friendLikes":0,"likes":1,"myFaves":1,"myLikes":0,"seriesId":"8970471"},
    {"faves":2,"friendFaves":0,"friendLikes":0,"likes":2,"myFaves":1,"myLikes":0,"seriesId":"184072"}
]
于 2012-04-06T20:45:12.120 に答える