简体   繁体   English

从 JSON 创建一个 object

[英]creating an object from JSON

I've seen a lot of different answers to this question and have tried applying their code to my project but none of these solutions seem to work for the data I have.对于这个问题,我看到了很多不同的答案,并尝试将他们的代码应用到我的项目中,但这些解决方案似乎都不适用于我拥有的数据。

I need to turn this output into several objects:我需要把这个 output 变成几个对象:

[{"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}},{"creature":{"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":8863412 [{"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}},{"creature":{"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”:8863412 539188,"attack":null,"defense":null,"action_points":null,"attack_cost":null}}] 539188,“攻击”:null,“防御”:null,“action_points”:null,“attack_cost”:null}}]

When I try jQuery.parseJSON(), it just gives me a bunch of [object Object]s but I can't refer to creature[1].id etc.当我尝试 jQuery.parseJSON() 时,它只给了我一堆 [object Object],但我不能引用 creature[1].id 等。

Again, I know this is a frequently asked question.同样,我知道这是一个常见问题。 I really have been through many other examples but they just didn't work out for me.我真的经历过很多其他的例子,但它们对我来说都行不通。

Thank you.谢谢你。

Each object has one property ( creature ) with another object as it's value.每个 object 都有一个属性( creature ),另一个 object 作为它的值。

result_of_parsing_json[1].creature.id

Your code seems perfectly valid.您的代码似乎完全有效。 Try this jsfiddle .试试这个 jsfiddle

var creatures = $.parseJSON(yourJSONString);

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

Do you need any specific clarifications?您需要任何具体说明吗?

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
*/

Each creature is nested within another object, and since it's an array of objects (that contain the creature), you have to iterate over it with a for loop, to make use of it.每个生物都嵌套在另一个 object 中,因为它是一个对象数组(包含该生物),您必须使用for循环对其进行迭代才能使用它。

Your parsing of the JSON, then, was most likely correct, but the logic that came afterwards was not (at a total guess).那么,您对 JSON 的解析很可能是正确的,但之后出现的逻辑却不是(完全是猜测)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM