对于这个问题,我看到了很多不同的答案,并尝试将他们的代码应用到我的项目中,但这些解决方案似乎都不适用于我拥有的数据。
我需要将此输出转换为多个对象:
[{"creature":{"id":1,"name":"R.I.P.","sprite_location":null,"health_points":0,"attack":0,"defense":0,"action_points ":0,"attack_cost":0}},{"creature":{"id":2,"name":"R.I.P.","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, “名称”:“公牛。”,“sprite_location”:“http://chunkofwhat.com/games/Parousia/sprites/bull.gif”,“health_points”:50,“攻击”:8,“防御”:20 ,"action_points":9,"attack_cost":5}},{"creature":{"id":4,"name":"吞下去。","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,"防御":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}}]
当我尝试 jQuery.parseJSON() 时,它只给了我一堆 [object Object],但我无法引用 Brevets[1].id 等。
再说一遍,我知道这是一个常见问题。我确实经历过很多其他例子,但它们对我来说并不奏效。
谢谢。
最佳答案
每个对象都有一个属性 (creature
),其值为另一个对象。
result_of_parsing_json[1].creature.id
关于javascript - 从 JSON 创建对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10043336/