0

我的 json 文件打印得很漂亮:

{u'count': 80,
 u'data': [{u'comment': {u'activity_id': 41638,
                         u'date': u'4/2/2013 11:07:29 PM',
                         u'id': 3230,
                         u'message': u'I echo that.  Thanks for jumping in at any moment without hesitation to take the lead on TMO deliverables; you have improved our relationship with an extremely valuable client.',
                         u'user': {u'first_name': u'Brent',
                                   u'id': 4863,
                                   u'last_name': u'Camalich',
                                   u'picture': u'http://lunchbox.youearnedit.com/profile/4863/image/'}}},
           {u'comment': {u'activity_id': 42577,
                         u'date': u'4/5/2013 12:29:16 AM',
                         u'id': 3384,
                         u'message': u'Thanks Greg!  Go team Gamecenter!',
                         u'user': {u'first_name': u'Malik',
                                   u'id': 7581,
                                   u'last_name': u'Jones',
                                   u'picture': u'http://lunchbox.youearnedit.com/profile/7581/image/'}}},
           {u'comment': {u'activity_id': 42578,
                         u'date': u'4/5/2013 12:29:54 AM',
                         u'id': 3385,
                         u'message': u'Thanks Josh!!  Appreciate the points!  Let me know if you ever need anything.',
                         u'user': {u'first_name': u'Malik',
                                   u'id': 7581,
                                   u'last_name': u'Jones',
                                   u'picture': u'http://lunchbox.youearnedit.com/profile/7581/image/'}}}],
 u'page': 1,
 u'page_count': 4,
 u'page_size': 25}

我可以返回 json 文件的顶级部分,例如count, page, page_count,page_size但我很难获得activity_ids 的列表。之后,我计划从列表中随机挑选以查找评论。任何帮助表示赞赏

#!/bin/python

import requests, json
from pprint import pprint

comments_url = "https://lunchbox:9e7cb94db13448209b1a66e71e145b7d@api.youearnedit.com/comments"
r = requests.get(comments_url)

json_data = r.json()

print "Comment Count"
print json_data["count"]

pprint(json_data)

activity_ids = [comment['activity_id'] for comment in json_data[1]['comments']]

for activity_id in activity_ids:
    print activity_id
4

1 回答 1

0

知道了。

for item in json_data['data']:
    comment = item['comment']
    print comment['activity_id']
于 2013-05-09T08:25:39.970 回答