0

我有从 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 时,只打印第一组数据(用于用户“测试”)。我怎样才能解决这个问题?

4

1 回答 1

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])
于 2012-12-19T21:57:57.647 回答