How to iterate over this Json data in rails? -


i'm trying iterate on json data want items out of "data tried doing

@response =httparty.get(("http://ddragon.leagueoflegends.com/cdn/5.15.1/data/en_us/item.json")) puts @response["data"].each |item|     puts item end 

also want able iterate them without having know ids prior example first item "1001" don't want have enter manually

but says can't find '[]' nil:nilclass before mentions json below doesn't finish because took sample there lot more items , if paste , on 1000 lines.

"type":"item", "version":"5.15.1", "basic":{}, "data":{         "1001":{                 "name":"boots of speed",                 "group":"bootsnormal",                 "description":"<grouplimit>limited 1.</grouplimit><br><br>                                                 <unique>unique passive - enhanced movement:</unique> +25 movement speed<br><br>                 <i>(unique passives 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":{                 }         }, 

four problems:

  1. you not parsing response body json.parse
  2. you using constant name (starting capital letter) variable name (@response), shouldn't.
  3. unnecessary puts on iteration.
  4. the value of data not array, rather hash. should iterate on key/value pairs.

solution:

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

Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -