如何在 rails 中迭代此 Json 数据?

How to iterate over this Json data in rails?

我正在尝试迭代此 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":{
                }
        },

四个问题:

  1. 您没有使用 JSON.parse
  2. 解析响应正文
  3. 您正在使用常量名称(以大写字母开头)作为变量名称 (@Response),您不应该这样做。
  4. 迭代时不需要 puts
  5. data的值不是数组,而是另一个散列。因此,您应该将其迭代为 key/value 对。

解决方案:

@response = JSON.parse(HTTParty.get(your_url).body)
@response["data"].each do |key, value|
  puts key
  puts value
end

我以前做过类似的事情,也许这里可以帮助你 link - Reading Json using Ruby