我从FQL查询返回以下内容:
posts_fql = graph.fql({'posts':"SELECT uid, name, pic, username FROM user WHERE is_app_user=1 AND uid IN (SELECT uid2 FROM friend WHERE uid1 = me())"})
print ast.literal_eval(json.dumps(posts_fql))
[
{
'fql_result_set': [
{
'username': 'sahithi.akasapu',
'pic': 'http://profile.ak.fbcdn.net/hprofile-ak-ash3/173521_1033451070_925702476_s.jpg',
'uid': 1033451070,
'name': 'Sahithi Akasapu'
},
{
'username': 'joe.kaster.73',
'pic': 'http://profile.ak.fbcdn.net/static-ak/rsrc.php/v1/yh/r/C5yt7Cqf3zU.jpg',
'uid': 100004855536430,
'name': 'Joe Kaster'
}
],
'name': 'posts'
}
]
我正在尝试使用以下方法在python中创建一个列表:
user_list= []
for user in posts_fql:
uid = user['fql_result_set'][0]['uid']
username = user['fql_result_set'][0]['username']
name = user['fql_result_set'][0]['name']
pic = user['fql_result_set'][0]['pic']
user_list.append([uid, username, name, pic])
当我打印user_list时,只打印第一组数据(用于'test')。我该如何解决这个问题?
答案 0 :(得分:1)
因为您的数据结构错误。 fql_result_set
位于用户列表之外。它是:
posts_fql = [{ 'fql_result_set' : [{user_dict1}, {user_dict2}] }]
所以,你真正想要的是:
user_list= []
for user in posts_fql[0]['fql_result_set']:
uid = user['uid']
username = user['username']
name = user['name']
pic = user['pic']
user_list.append([uid, username, name, pic])