通过JSON响应迭代Facebook Graph

时间:2014-04-29 09:17:15

标签: ruby json api facebook-graph-api

我试图迭代来自Facebook Graph api的返回响应

def get_feed
  uri = URI(FACEBOOK_URL)
  response = HTTParty.get(uri)
  results = JSON.parse(response.body)['data']
  puts formatted_data(results)
end

def formatted_data(results)
return unless results
formatted = results['data'].each do |d|
      unless d.nil? 
       {
        message: d['message'],
       }
     end
  formatted.delete_if {|x| x.nil?}
end
end

响应非常大,所以如果它有帮助,这里有一个片段

{
"data": [
  {
     "id": "197958940234297_827831980580320",
     "from": {
        "category": "Amateur sports team",
        "category_list": [
           {
              "id": "189018581118681",
              "name": "Sports Club"
           },
           {
              "id": "139721016091877",
              "name": "Outdoor Recreation"
           },
           {
              "id": "109615542448700",
              "name": "Physical Fitness"
           }
        ],
        "name": "Varsity Vandals",
        "id": "197958940234297"
     },
     "to": {
        "data": [
           {
              "id": "668983363",
              "name": "Heather Walker"
           },
           {
              "id": "638195502",
              "name": "Emma Williams"
           },
           {
              "id": "1286337937",
              "name": "Becky Williams"
           }
        ]
     },
     "with_tags": {
        "data": [
           {
              "id": "668983363",
              "name": "Heather Walker"
           },
           {
              "id": "638195502",
              "name": "Emma Williams"
           },
           {
              "id": "1286337937",
              "name": "Becky Williams"
           }
        ]
     },
     "message": "Great turnout for the women's intro session today. Cool to have a women's game and a men's game running side by side. Touch is for all.",
     "picture": "https://fbcdn-photos-f-a.akamaihd.net/hphotos-ak-prn2/t1.0-0/1507550_827829843913867_410211203232735862_s.jpg",
     "link": "https://www.facebook.com/photo.php?fbid=827829843913867&set=pcb.827831980580320&type=1&relevant_count=2",
     "icon": "https://fbstatic-a.akamaihd.net/rsrc.php/v2/yz/r/StEh3RhPvjk.gif",
     "actions": [
        {
           "name": "Comment",
           "link": "https://www.facebook.com/197958940234297/posts/827831980580320"
        },
        {
           "name": "Like",
           "link": "https://www.facebook.com/197958940234297/posts/827831980580320"
        }
     ],
     "privacy": {
        "value": ""
     },

我收到错误

TypeError: no implicit conversion of String into Integer

目前我只想从JSON对象中提取所有消息...我是否正确处理提取

任何帮助表示赞赏

由于

3 个答案:

答案 0 :(得分:2)

我试过你的代码,我改变你要求移动formatted.delete_if {|x| x.nil?}离开循环,如下所示,因为formatted将在循环中nil

    def formatted_data(results)
      return unless results
      formatted = results['data'].each do |d|
          unless d.nil? 
           {
            message: d['message'],
           }
         end

       end
    formatted.delete_if {|x| x.nil?}
    end

答案 1 :(得分:1)

我这样做:

def get_feed
  uri = URI(FACEBOOK_URL)
  response = HTTParty.get(uri)

  messages = format_data(response)
  for message in messages do
      puts message
  end
end

def format_data(response, new_data = [])
    if response.present?
        results = JSON.parse(response)
        for result in results do
           new_data << result[:data][:message]  if result[:data][:message].present?
        end
        return new_data #-> array of messages
    end
end

答案 2 :(得分:1)

你确定你没有两次使用数据密钥吗?

主方法中的

results = JSON.parse(response.body)['data']和您的formatted_data方法中的formatted = results['data'].each

可能在思考?

def def formatted_data(results)
  return unless results
  results['data'].map {|m| {message: m['message']} }.compact
end