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" :"Bull.","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,"防御":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}},{ "生物":{"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,"防御":15,"action_points":9,"attack_cost":3}},{"creature":{"id":6,"name":null,"sprite_location":null,"health_points": null,"攻击":null,"防御":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,"防御":15,"action_points":9,"attack_cost":3}},{"creature":{"id":6,"name":null,"sprite_location":null,"health_points": null,"攻击":null,"防御":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],但我不能引用 bio[1].id 等。

同样,我知道这是一个经常被问到的问题。我真的经历过很多其他的例子,但它们对我来说并不奏效。

谢谢你。

4

3 回答 3

2

每个对象都有一个属性 ( creature),另一个对象作为它的值。

result_of_parsing_json[1].creature.id
于 2012-04-06T12:18:02.440 回答
0

您的代码似乎完全有效。试试这个 jsfiddle

var creatures = $.parseJSON(yourJSONString);

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

您需要任何具体说明吗?

于 2012-04-06T12:23:27.490 回答
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 回答