【发布时间】:2015-08-16 15:01:16
【问题描述】:
我正在尝试迭代这个 Json 数据我想从“我尝试做的数据中获取项目”
@Response =HTTParty.get(("http://ddragon.leagueoflegends.com/cdn/5.15.1/data/en_US/item.json"))
puts @Response["data"].each do |item|
puts item
end
我还希望能够在不必事先知道 IDS 的情况下迭代它们,例如第一项是“1001”我不想手动输入这些
但这只是说它找不到任何带有 '[]' 的 Nil:NilClass 同样在有人提到下面的 JSON 没有完成之前,因为我只拿了一个样本,还有很多项目,如果我要粘贴它,它很容易超过 1000 行。
"type":"item",
"version":"5.15.1",
"basic":{},
"data":{
"1001":{
"name":"Boots of Speed",
"group":"BootsNormal",
"description":"<groupLimit>Limited to 1.</groupLimit><br><br> <unique>UNIQUE Passive - Enhanced Movement:</unique> +25 Movement Speed<br><br> <i>(Unique Passives with the same name don't stack.)</i>",
"colloq":";",
"plaintext":"Slightly increases Movement Speed",
"into":[
"3006",
"3047",
"3020",
"3158",
"3111",
"3117",
"3009"
],
"image":{
"full":"1001.png",
"sprite":"item0.png",
"group":"item",
"x":0,
"y":0,
"w":48,
"h":48
},
"gold":{
"base":325,
"purchasable":true,
"total":325,
"sell":227
},
"tags":[
"Boots"
],
"stats":{
"FlatMovementSpeedMod":25.0
}
},
"1004":{
"name":"Faerie Charm",
"description":"<stats><mana>+25% Base Mana Regen </mana></stats>",
"colloq":";",
"plaintext":"Slightly increases Mana Regen",
"into":[
"3028",
"3070",
"3073",
"3114"
],
"image":{
"full":"1004.png",
"sprite":"item0.png",
"group":"item",
"x":48,
"y":0,
"w":48,
"h":48
},
"gold":{
"base":180,
"purchasable":true,
"total":180,
"sell":126
},
"tags":[
"ManaRegen"
],
"stats":{
}
},
【问题讨论】:
-
请花一些时间很好地缩进 JSON,它现在看起来不可读。
-
好的,现在应该会更好
-
你确定你的 Ruby 是正确的吗?看起来不对。您的 JSON 响应是否真的存储在 @Response 中?响应哈希的键是否应该像这样缺少右引号?
-
引号是固定的,但是响应是@Response 我添加了实际响应的代码只是为了显示(它是 HTTParty::response)
标签: ruby-on-rails json httparty