【发布时间】:2014-07-02 12:27:59
【问题描述】:
我正在使用fql gem 从 facebook 检索数据。原始的哈希数组是这样的。 Here。当我比较这三个哈希数组时,我想以这种方式获得最终结果:
{
"photo" => [
[0] {
"owner" : "1105762436",
"src_big" : "https://fbcdn-sphotos-b-a.akamaihd.net/hphotos-ak-xap1/t31.0-8/q71/s720x720/10273283_10203050474118531_5420466436365792507_o.jpg",
"caption" : "Rings...!!\n\nView Full Screen.",
"created" : 1398953040,
"modified" : 1398953354,
"like_info" : {
"can_like" : true,
"like_count" : 22,
"user_likes" : true
},
"comment_info" : {
"can_comment" : true,
"comment_count" : 2,
"comment_order" : "chronological"
},
"object_id" : "10203050474118531",
"pid" : "4749213500839034982"
}
],
"comment" => [
[0] {
"text" : "Wow",
"text_tags" : [],
"time" : 1398972853,
"likes" : 1,
"fromid" : "100001012753267",
"object_id" : "10203050474118531"
},
[1] {
"text" : "Woww..",
"text_tags" : [],
"time" : 1399059923,
"likes" : 0,
"fromid" : "100003167704574",
"object_id" : "10203050474118531"
}
],
"users" =>[
[0] {
"id": "1105762436",
"name": "Nilanjan Joshi",
"username": "NilaNJan219"
},
[1] {
"id": "1105762436",
"name": "Ashish Joshi",
"username": "NilaNJan219"
}
]
}
这是我的尝试:
datas = File.read('source2.json')
all_data = JSON.parse(datas)
photos = all_data[0]['fql_result_set'].group_by{|x| x['object_id']}.to_a
comments = all_data[1]['fql_result_set'].group_by{|x| x['object_id']}.to_a
@photos_comments = []
@comments_users = []
@photo_users = []
photos.each do |a|
comments.each do |b|
if a.first == b.first
@photos_comments << {'photo' => a.last, 'comment' => b.last}
else
@comments_users << {'photo' => a.last, 'comment' => ''} unless @photos_comments.include? (a.last)
end
end
end
@photo_users = @photos_comments | @comments_users
@photo_comment_users = {photos_comments: @photo_users }
这是我得到的final result
最终数组中仍然存在重复项。我有grouped byobject id 的数组,这在照片和评论数组之间很常见。但问题在于只拍摄那些有 cmets 的照片。我不知道如何找出没有 cmets 的照片。
另外为了找出评论者的详细信息,ive users 数组,comments 和users 之间的共同属性是fromid 和id。我也无法理解如何获取用户详细信息。
【问题讨论】:
标签: ruby arrays ruby-on-rails-4 hash fql.multiquery