0

この質問に対するさまざまな回答を見て、それらのコードを私のプロジェクトに適用しようとしましたが、これらの解決策はどれも私が持っているデータではうまくいかないようです。

この出力をいくつかのオブジェクトに変換する必要があります。

[{"creature":{"id":1,"name":"RIP","sprite_location":null,"health_points":0,"attack":0,"defense":0,"action_points":0 ,"attack_cost":0}},{"creature":{"id":2,"name":"RIP","sprite_location":"http://chunkofwhat.com/games/Parousia/sprites/rip. gif","health_points":0,"attack":0,"defense":0,"action_points":0,"attack_cost":0}},{"creature":{"id":3,"name" :"雄牛","sprite_location":"http://chunkofwhat.com/games/Parousia/sprites/bull.gif","health_points":50,"attack":8,"defense":20,"action_points ":9,"attack_cost":5}},{"クリーチャー":{"id":4,"name":"Swallow.","sprite_location":"http://chunkofwhat.com/games/Parousia/sprites/swallow.gif","health_points":30,"attack": 12,"defense":10,"action_points":13,"attack_cost":5}},{"creature":{"id":5,"name":"Kappa.","sprite_location":"http: //chunkofwhat.com/games/Parousia/sprites/kappa.gif","health_points":40,"attack":6,"defense":15,"action_points":9,"attack_cost":3}},{ "creature":{"id":6,"name":null,"sprite_location":null,"health_points":null,"attack":null,"defense":null,"action_points":null,"attack_cost" :ヌル}}]","sprite_location":"http://chunkofwhat.com/games/Parousia/sprites/swallow.gif","health_points":30,"attack":12,"defense":10,"action_points": 13,"attack_cost":5}},{"creature":{"id":5,"name":"Kappa.","sprite_location":"http://chunkofwhat.com/games/Parousia/sprites/ kappa.gif","health_points":40,"attack":6,"defense":15,"action_points":9,"attack_cost":3}},{"creature":{"id":6," name":null,"sprite_location":null,"health_points":null,"attack":null,"defense":null,"action_points":null,"attack_cost":null}}]","sprite_location":"http://chunkofwhat.com/games/Parousia/sprites/swallow.gif","health_points":30,"attack":12,"defense":10,"action_points": 13,"attack_cost":5}},{"creature":{"id":5,"name":"Kappa.","sprite_location":"http://chunkofwhat.com/games/Parousia/sprites/ kappa.gif","health_points":40,"attack":6,"defense":15,"action_points":9,"attack_cost":3}},{"creature":{"id":6," name":null,"sprite_location":null,"health_points":null,"attack":null,"defense":null,"action_points":null,"attack_cost":null}}]com/games/Parousia/sprites/swallow.gif","health_points":30,"attack":12,"defense":10,"action_points":13,"attack_cost":5}},{"creature": {"id":5,"name":"Kappa.","sprite_location":"http://chunkofwhat.com/games/Parousia/sprites/kappa.gif","health_points":40,"attack": 6,"defense":15,"action_points":9,"attack_cost":3}},{"creature":{"id":6,"name":null,"sprite_location":null,"health_points": null,"attack":null,"defense":null,"action_points":null,"attack_cost":null}}]com/games/Parousia/sprites/swallow.gif","health_points":30,"attack":12,"defense":10,"action_points":13,"attack_cost":5}},{"creature": {"id":5,"name":"Kappa.","sprite_location":"http://chunkofwhat.com/games/Parousia/sprites/kappa.gif","health_points":40,"attack": 6,"defense":15,"action_points":9,"attack_cost":3}},{"creature":{"id":6,"name":null,"sprite_location":null,"health_points": null,"attack":null,"defense":null,"action_points":null,"attack_cost":null}}]attack_cost":5}},{"creature":{"id":5,"name":"Kappa.","sprite_location":"http://chunkofwhat.com/games/Parousia/sprites/kappa.gif ","health_points":40,"attack":6,"defense":15,"action_points":9,"attack_cost":3}},{"creature":{"id":6,"name": null,"sprite_location":null,"health_points":null,"attack":null,"defense":null,"action_points":null,"attack_cost":null}}]attack_cost":5}},{"creature":{"id":5,"name":"Kappa.","sprite_location":"http://chunkofwhat.com/games/Parousia/sprites/kappa.gif ","health_points":40,"attack":6,"defense":15,"action_points":9,"attack_cost":3}},{"creature":{"id":6,"name": null,"sprite_location":null,"health_points":null,"attack":null,"defense":null,"action_points":null,"attack_cost":null}}]attack_cost":3}},{"creature":{"id":6,"name":null,"sprite_location":null,"health_points":null,"attack":null,"defense":null," action_points":null,"attack_cost":null}}]attack_cost":3}},{"creature":{"id":6,"name":null,"sprite_location":null,"health_points":null,"attack":null,"defense":null," action_points":null,"attack_cost":null}}]

jQuery.parseJSON() を試すと、たくさんの [object Object] が返されるだけですが、creative[1].id などを参照できません。

繰り返しますが、これはよくある質問です。私は実際に他の多くの例を経験してきましたが、うまくいきませんでした.

ありがとうございました。

4

3 に答える 3

2

各オブジェクトにはcreature、値として別のオブジェクトを持つ 1 つのプロパティ ( ) があります。

result_of_parsing_json[1].creature.id
于 2012-04-06T12:18:02.440 に答える
0
var creatures = JSON.parse('big_json_string');

for (var i = 0; i < creatures.length; i++) {
    var creature = creatures[i].creature; // this is how your object is formatted

    console.log(creature.name);
}

/*
 R.I.P.
 R.I.P.
 Bull.
 Swallow.
 Kappa.
 null
*/

各クリーチャーは別のオブジェクト内にネストされており、オブジェクトの配列(クリーチャーを含む)であるため、それを使用するには、forループを使用してクリーチャーを反復処理する必要があります。

したがって、JSONの解析はおそらく正しいものでしたが、その後に行われたロジックは正しくありませんでした(全体的な推測では)。

于 2012-04-06T12:27:13.877 に答える
0

あなたのコードは完全に有効なようです。このjsfiddleを試してください。

var creatures = $.parseJSON(yourJSONString);

alert(creatures[0].creature.name);​ // alerts "R.I.P"

特定の説明が必要ですか?

于 2012-04-06T12:23:27.490 に答える