Ruby - iterate over parsed JSON

后端 未结 1 1163
感情败类
感情败类 2020-12-17 09:19

I\'m trying to iterate of a parsed JSON response from reddit\'s API.

I\'ve done some googling and seems others have had this issue but none of the solutions seem to

相关标签:
1条回答
  • 2020-12-17 09:26

    You're trying to iterate over data, which is a hash, not a list. You need to get the children array from your JSON object by data['data']['children']

    require "net/http"
    require "uri"
    require "json"
    
    uri = URI.parse("http://www.reddit.com/user/brain_poop/comments/.json")
    
    response = Net::HTTP.get_response(uri)
    
    data = JSON.parse(response.body)
    
    
    data['data']['children'].each do |child|
        puts child['data']['body']
    end
    
    0 讨论(0)
提交回复
热议问题