我使用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 by
object id
数组comments
,这是照片和注释数组之间的共同点。但问题是它只拍摄那些有评论的照片。我没有办法找到没有评论的照片。
另外,为了查找已评论的人员的详细信息,ive users数组以及users
和fromid
之间的公共属性为id
和{{1}}。我也无法理解如何获取用户详细信息。
答案 0 :(得分:1)
我认为这就是你想要的:
photos = all_data[0]['fql_result_set']
comments = all_data[1]['fql_result_set'].group_by{|x| x['object_id']}
@photo_comment_users = photos.map do |p|
{ 'photo' => p, 'comment' => comments[p['object_id']] || '' }
end
对于每个photo
,所有comments
使用相同的object_id
,如果不存在,则返回''
。
如果您也想连接用户,可以按ID映射,并通过评论选择相关的:
users = Hash[all_data[2]['fql_result_set'].map {|x| [x['id'], x]}]
@photo_comment_users = photos.map do |p|
{ 'photo' => p, 'comment' => comments[p['object_id']] || '',
'user' => (comments[p['object_id']] || []).map {|c| users[c['formid']]} }
end