2012-04-06 5 views
0

からオブジェクトを作成します。は、私はこの質問に異なる答えをたくさん見てきたし、私のプロジェクトに自分のコードを適用しようとしているが、これらのソリューションのどれも私が持っているデータのために働くように見えるんJSON

[{ "生き物":{ "ID":1、 "名前": "RIP"、 "sprite_location":ヌル、 "health_points":0

私はいくつかのオブジェクトにこの出力を有効にする必要があり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": "Bull。"、 "sprite_location": "http://chunkofwhat.com/games/Parousia/sprites/bull.gif"、 "health_points "、"攻撃 ":8、"防御 ":20、" action_points ":9、" attack_cost ":5}}、{" creature ":{" id ":4、" name ":" Swallow " 、 "health_points":30、 "attack":12、 "defense":10、 "action_points":13、 "spy_location": "http://chunkofwhat.com/games/Parousia/sprites/swallow.gif" 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、 "防御":null、 "action_points":null、 "attack_cost":null}}]

jQuery.parseJSON()を試すと、ちょうど私の[オブジェクトのオブジェクト] Sの束を与えるが、私は生き物を参照することはできません[1] .IDなど

繰り返しますが、私が知っている、これは頻繁に質問をしています。私は実際に多くの他の例を経てきましたが、彼らは私のためにうまくいっていませんでした。

ありがとうございます。

答えて

2

各オブジェクトは、それは、値のような別のオブジェクトと一つの特性(creature)を有しています。

result_of_parsing_json[1].creature.id 
0

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

var creatures = $.parseJSON(yourJSONString); 

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

具体的な説明が必要ですか?

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のあなたの構文解析、そして、最も可能性の高い正しかったが、その後に来たロジックは、(総推測では)ありませんでした。