Python 多个JSON根元素-Twitter API流

Python 多个JSON根元素-Twitter API流,python,json,python-3.x,Python,Json,Python 3.x,我以前看到有人问过这个问题,但答案对我没有帮助,所以我将在这里试一试: 我使用Twython从twitterapi流式传输实时推文。这些推文通过以下方式附加到json文件: with open('fetched_tweets.json','a') as tf: y = json.dump(data,tf) 我的问题: 以下是来自twitter API的JSON响应示例: { "text": "RT @PostGradProblem: In preparatio

我以前看到有人问过这个问题,但答案对我没有帮助,所以我将在这里试一试:

我使用Twython从twitterapi流式传输实时推文。这些推文通过以下方式附加到json文件:

with open('fetched_tweets.json','a') as tf:
               y = json.dump(data,tf)
我的问题:

以下是来自twitter API的JSON响应示例:

{
  "text": "RT @PostGradProblem: In preparation for the NFL lockout, I will be spending twice as much time analyzing my fantasy baseball team during ...", 
  "truncated": true, 
  "in_reply_to_user_id": null, 
  "in_reply_to_status_id": null, 
  "favorited": false, 
  "source": "<a href=\"http://twitter.com/\" rel=\"nofollow\">Twitter for iPhone</a>", 
  "in_reply_to_screen_name": null, 
  "in_reply_to_status_id_str": null, 
  "id_str": "54691802283900928", 
  "entities": {
        "user_mentions": [
              {
                    "indices": [
                          3, 
                          19
                    ], 
                    "screen_name": "PostGradProblem", 
                    "id_str": "271572434", 
                    "name": "PostGradProblems", 
                    "id": 271572434
              }
        ], 
        "urls": [ ], 
        "hashtags": [ ]
  }, 
  "contributors": null, 
  "retweeted": false, 
  "in_reply_to_user_id_str": null, 
  "place": null, 
  "retweet_count": 4, 
  "created_at": "Sun Apr 03 23:48:36 +0000 2011", 
  "retweeted_status": {
        "text": "In preparation for the NFL lockout, I will be spending twice as much time analyzing my fantasy baseball team during company time. #PGP", 
        "truncated": false, 
        "in_reply_to_user_id": null, 
        "in_reply_to_status_id": null, 
        "favorited": false, 
        "source": "<a href=\"http://www.hootsuite.com\" rel=\"nofollow\">HootSuite</a>", 
        "in_reply_to_screen_name": null, 
        "in_reply_to_status_id_str": null, 
        "id_str": "54640519019642881", 
        "entities": {
              "user_mentions": [ ], 
              "urls": [ ], 
              "hashtags": [
                    {
                          "text": "PGP", 
                          "indices": [
                                130, 
                                134
                          ]
                    }
              ]
        }, 
        "contributors": null, 
        "retweeted": false, 
        "in_reply_to_user_id_str": null, 
        "place": null, 
        "retweet_count": 4, 
        "created_at": "Sun Apr 03 20:24:49 +0000 2011", 
        "user": {
              "notifications": null, 
              "profile_use_background_image": true, 
              "statuses_count": 31, 
              "profile_background_color": "C0DEED", 
              "followers_count": 3066, 
              "profile_image_url": "http://a2.twimg.com/profile_images/1285770264/PGP_normal.jpg", 
              "listed_count": 6, 
              "profile_background_image_url": "http://a3.twimg.com/a/1301071706/images/themes/theme1/bg.png", 
              "description": "", 
              "screen_name": "PostGradProblem", 
              "default_profile": true, 
              "verified": false, 
              "time_zone": null, 
              "profile_text_color": "333333", 
              "is_translator": false, 
              "profile_sidebar_fill_color": "DDEEF6", 
              "location": "", 
              "id_str": "271572434", 
              "default_profile_image": false, 
              "profile_background_tile": false, 
              "lang": "en", 
              "friends_count": 21, 
              "protected": false, 
              "favourites_count": 0, 
              "created_at": "Thu Mar 24 19:45:44 +0000 2011", 
              "profile_link_color": "0084B4", 
              "name": "PostGradProblems", 
              "show_all_inline_media": false, 
              "follow_request_sent": null, 
              "geo_enabled": false, 
              "profile_sidebar_border_color": "C0DEED", 
              "url": null, 
              "id": 271572434, 
              "contributors_enabled": false, 
              "following": null, 
              "utc_offset": null
        }, 
        "id": 54640519019642880, 
        "coordinates": null, 
        "geo": null
  }, 
  "user": {
        "notifications": null, 
        "profile_use_background_image": true, 
        "statuses_count": 351, 
        "profile_background_color": "C0DEED", 
        "followers_count": 48, 
        "profile_image_url": "http://a1.twimg.com/profile_images/455128973/gCsVUnofNqqyd6tdOGevROvko1_500_normal.jpg", 
        "listed_count": 0, 
        "profile_background_image_url": "http://a3.twimg.com/a/1300479984/images/themes/theme1/bg.png", 
        "description": "watcha doin in my waters?", 
        "screen_name": "OldGREG85", 
        "default_profile": true, 
        "verified": false, 
        "time_zone": "Hawaii", 
        "profile_text_color": "333333", 
        "is_translator": false, 
        "profile_sidebar_fill_color": "DDEEF6", 
        "location": "Texas", 
        "id_str": "80177619", 
        "default_profile_image": false, 
        "profile_background_tile": false, 
        "lang": "en", 
        "friends_count": 81, 
        "protected": false, 
        "favourites_count": 0, 
        "created_at": "Tue Oct 06 01:13:17 +0000 2009", 
        "profile_link_color": "0084B4", 
        "name": "GG", 
        "show_all_inline_media": false, 
        "follow_request_sent": null, 
        "geo_enabled": false, 
        "profile_sidebar_border_color": "C0DEED", 
        "url": null, 
        "id": 80177619, 
        "contributors_enabled": false, 
        "following": null, 
        "utc_offset": -36000
  }, 
  "id": 54691802283900930, 
  "coordinates": null, 
  "geo": null
} {
  "text": "RT @PostGradProblem: In preparation for the NFL lockout, I will be spending twice as much time analyzing my fantasy baseball team during ...", 
  "truncated": true, 
  "in_reply_to_user_id": null, 
  "in_reply_to_status_id": null, 
  "favorited": false, 
  "source": "<a href=\"http://twitter.com/\" rel=\"nofollow\">Twitter for iPhone</a>", 
  "in_reply_to_screen_name": null, 
  "in_reply_to_status_id_str": null, 
  "id_str": "54691802283900928", 
  "entities": {
        "user_mentions": [
              {
                    "indices": [
                          3, 
                          19
                    ], 
                    "screen_name": "PostGradProblem", 
                    "id_str": "271572434", 
                    "name": "PostGradProblems", 
                    "id": 271572434
              }
        ], 
        "urls": [ ], 
        "hashtags": [ ]
  }, 
  "contributors": null, 
  "retweeted": false, 
  "in_reply_to_user_id_str": null, 
  "place": null, 
  "retweet_count": 4, 
  "created_at": "Sun Apr 03 23:48:36 +0000 2011", 
  "retweeted_status": {
        "text": "In preparation for the NFL lockout, I will be spending twice as much time analyzing my fantasy baseball team during company time. #PGP", 
        "truncated": false, 
        "in_reply_to_user_id": null, 
        "in_reply_to_status_id": null, 
        "favorited": false, 
        "source": "<a href=\"http://www.hootsuite.com\" rel=\"nofollow\">HootSuite</a>", 
        "in_reply_to_screen_name": null, 
        "in_reply_to_status_id_str": null, 
        "id_str": "54640519019642881", 
        "entities": {
              "user_mentions": [ ], 
              "urls": [ ], 
              "hashtags": [
                    {
                          "text": "PGP", 
                          "indices": [
                                130, 
                                134
                          ]
                    }
              ]
        }, 
        "contributors": null, 
        "retweeted": false, 
        "in_reply_to_user_id_str": null, 
        "place": null, 
        "retweet_count": 4, 
        "created_at": "Sun Apr 03 20:24:49 +0000 2011", 
        "user": {
              "notifications": null, 
              "profile_use_background_image": true, 
              "statuses_count": 31, 
              "profile_background_color": "C0DEED", 
              "followers_count": 3066, 
              "profile_image_url": "http://a2.twimg.com/profile_images/1285770264/PGP_normal.jpg", 
              "listed_count": 6, 
              "profile_background_image_url": "http://a3.twimg.com/a/1301071706/images/themes/theme1/bg.png", 
              "description": "", 
              "screen_name": "PostGradProblem", 
              "default_profile": true, 
              "verified": false, 
              "time_zone": null, 
              "profile_text_color": "333333", 
              "is_translator": false, 
              "profile_sidebar_fill_color": "DDEEF6", 
              "location": "", 
              "id_str": "271572434", 
              "default_profile_image": false, 
              "profile_background_tile": false, 
              "lang": "en", 
              "friends_count": 21, 
              "protected": false, 
              "favourites_count": 0, 
              "created_at": "Thu Mar 24 19:45:44 +0000 2011", 
              "profile_link_color": "0084B4", 
              "name": "PostGradProblems", 
              "show_all_inline_media": false, 
              "follow_request_sent": null, 
              "geo_enabled": false, 
              "profile_sidebar_border_color": "C0DEED", 
              "url": null, 
              "id": 271572434, 
              "contributors_enabled": false, 
              "following": null, 
              "utc_offset": null
        }, 
        "id": 54640519019642880, 
        "coordinates": null, 
        "geo": null
  }, 
  "user": {
        "notifications": null, 
        "profile_use_background_image": true, 
        "statuses_count": 351, 
        "profile_background_color": "C0DEED", 
        "followers_count": 48, 
        "profile_image_url": "http://a1.twimg.com/profile_images/455128973/gCsVUnofNqqyd6tdOGevROvko1_500_normal.jpg", 
        "listed_count": 0, 
        "profile_background_image_url": "http://a3.twimg.com/a/1300479984/images/themes/theme1/bg.png", 
        "description": "watcha doin in my waters?", 
        "screen_name": "OldGREG85", 
        "default_profile": true, 
        "verified": false, 
        "time_zone": "Hawaii", 
        "profile_text_color": "333333", 
        "is_translator": false, 
        "profile_sidebar_fill_color": "DDEEF6", 
        "location": "Texas", 
        "id_str": "80177619", 
        "default_profile_image": false, 
        "profile_background_tile": false, 
        "lang": "en", 
        "friends_count": 81, 
        "protected": false, 
        "favourites_count": 0, 
        "created_at": "Tue Oct 06 01:13:17 +0000 2009", 
        "profile_link_color": "0084B4", 
        "name": "GG", 
        "show_all_inline_media": false, 
        "follow_request_sent": null, 
        "geo_enabled": false, 
        "profile_sidebar_border_color": "C0DEED", 
        "url": null, 
        "id": 80177619, 
        "contributors_enabled": false, 
        "following": null, 
        "utc_offset": -36000
  }, 
  "id": 54691802283900930, 
  "coordinates": null, 
  "geo": null
} {{
  "text": "RT @PostGradProblem: In preparation for the NFL lockout, I will be spending twice as much time analyzing my fantasy baseball team during ...", 
  "truncated": true, 
  "in_reply_to_user_id": null, 
  "in_reply_to_status_id": null, 
  "favorited": false, 
  "source": "<a href=\"http://twitter.com/\" rel=\"nofollow\">Twitter for iPhone</a>", 
  "in_reply_to_screen_name": null, 
  "in_reply_to_status_id_str": null, 
  "id_str": "54691802283900928", 
  "entities": {
        "user_mentions": [
              {
                    "indices": [
                          3, 
                          19
                    ], 
                    "screen_name": "PostGradProblem", 
                    "id_str": "271572434", 
                    "name": "PostGradProblems", 
                    "id": 271572434
              }
        ], 
        "urls": [ ], 
        "hashtags": [ ]
  }, 
  "contributors": null, 
  "retweeted": false, 
  "in_reply_to_user_id_str": null, 
  "place": null, 
  "retweet_count": 4, 
  "created_at": "Sun Apr 03 23:48:36 +0000 2011", 
  "retweeted_status": {
        "text": "In preparation for the NFL lockout, I will be spending twice as much time analyzing my fantasy baseball team during company time. #PGP", 
        "truncated": false, 
        "in_reply_to_user_id": null, 
        "in_reply_to_status_id": null, 
        "favorited": false, 
        "source": "<a href=\"http://www.hootsuite.com\" rel=\"nofollow\">HootSuite</a>", 
        "in_reply_to_screen_name": null, 
        "in_reply_to_status_id_str": null, 
        "id_str": "54640519019642881", 
        "entities": {
              "user_mentions": [ ], 
              "urls": [ ], 
              "hashtags": [
                    {
                          "text": "PGP", 
                          "indices": [
                                130, 
                                134
                          ]
                    }
              ]
        }, 
        "contributors": null, 
        "retweeted": false, 
        "in_reply_to_user_id_str": null, 
        "place": null, 
        "retweet_count": 4, 
        "created_at": "Sun Apr 03 20:24:49 +0000 2011", 
        "user": {
              "notifications": null, 
              "profile_use_background_image": true, 
              "statuses_count": 31, 
              "profile_background_color": "C0DEED", 
              "followers_count": 3066, 
              "profile_image_url": "http://a2.twimg.com/profile_images/1285770264/PGP_normal.jpg", 
              "listed_count": 6, 
              "profile_background_image_url": "http://a3.twimg.com/a/1301071706/images/themes/theme1/bg.png", 
              "description": "", 
              "screen_name": "PostGradProblem", 
              "default_profile": true, 
              "verified": false, 
              "time_zone": null, 
              "profile_text_color": "333333", 
              "is_translator": false, 
              "profile_sidebar_fill_color": "DDEEF6", 
              "location": "", 
              "id_str": "271572434", 
              "default_profile_image": false, 
              "profile_background_tile": false, 
              "lang": "en", 
              "friends_count": 21, 
              "protected": false, 
              "favourites_count": 0, 
              "created_at": "Thu Mar 24 19:45:44 +0000 2011", 
              "profile_link_color": "0084B4", 
              "name": "PostGradProblems", 
              "show_all_inline_media": false, 
              "follow_request_sent": null, 
              "geo_enabled": false, 
              "profile_sidebar_border_color": "C0DEED", 
              "url": null, 
              "id": 271572434, 
              "contributors_enabled": false, 
              "following": null, 
              "utc_offset": null
        }, 
        "id": 54640519019642880, 
        "coordinates": null, 
        "geo": null
  }, 
  "user": {
        "notifications": null, 
        "profile_use_background_image": true, 
        "statuses_count": 351, 
        "profile_background_color": "C0DEED", 
        "followers_count": 48, 
        "profile_image_url": "http://a1.twimg.com/profile_images/455128973/gCsVUnofNqqyd6tdOGevROvko1_500_normal.jpg", 
        "listed_count": 0, 
        "profile_background_image_url": "http://a3.twimg.com/a/1300479984/images/themes/theme1/bg.png", 
        "description": "watcha doin in my waters?", 
        "screen_name": "OldGREG85", 
        "default_profile": true, 
        "verified": false, 
        "time_zone": "Hawaii", 
        "profile_text_color": "333333", 
        "is_translator": false, 
        "profile_sidebar_fill_color": "DDEEF6", 
        "location": "Texas", 
        "id_str": "80177619", 
        "default_profile_image": false, 
        "profile_background_tile": false, 
        "lang": "en", 
        "friends_count": 81, 
        "protected": false, 
        "favourites_count": 0, 
        "created_at": "Tue Oct 06 01:13:17 +0000 2009", 
        "profile_link_color": "0084B4", 
        "name": "GG", 
        "show_all_inline_media": false, 
        "follow_request_sent": null, 
        "geo_enabled": false, 
        "profile_sidebar_border_color": "C0DEED", 
        "url": null, 
        "id": 80177619, 
        "contributors_enabled": false, 
        "following": null, 
        "utc_offset": -36000
  }, 
  "id": 54691802283900930, 
  "coordinates": null, 
  "geo": null
}{"Another JSON response}{"Another JSON response"}
{
“文本”:“RT@PostgradeProblem:为NFL停摆做准备,我将在……期间花两倍的时间分析我的梦幻棒球队,
“截断”:正确,
“in_reply_to_user_id”:null,
“在对状态id的回复中”:null,
“偏爱”:错误,
“来源”:“,
“回复至屏幕名称”为空,
“在对状态的回复中”id“str”:空,
“id_街”:“54691802283900928”,
“实体”:{
“用户_提到”:[
{
“指数”:[
3.
19
], 
“屏幕名称”:“研究生问题”,
“id_街”:“271572434”,
“姓名”:“研究生问题”,
“id”:271572434
}
], 
“URL”:[],
“hashtags”:[]
}, 
“贡献者”:空,
“转发”:错误,
“在对用户id的回复中”:null,
“地点”:空,
“转发次数”:4,
“创建时间”:“2011年4月3日星期日23:48:36+0000”,
“转发状态”:{
“文本”:“为了准备NFL停摆,我将在公司时间内花两倍的时间分析我的梦幻棒球队。”,
“截断”:false,
“in_reply_to_user_id”:null,
“在对状态id的回复中”:null,
“偏爱”:错误,
“来源”:“,
“回复至屏幕名称”为空,
“在对状态的回复中”id“str”:空,
“id_街”:“54640519019642881”,
“实体”:{
“用户_提及”:[],
“URL”:[],
“hashtags”:[
{
“文本”:“PGP”,
“指数”:[
130, 
134
]
}
]
}, 
“贡献者”:空,
“转发”:错误,
“在对用户id的回复中”:null,
“地点”:空,
“转发次数”:4,
“创建时间”:“2011年4月3日星期日20:24:49+0000”,
“用户”:{
“通知”:空,
“profile\u use\u background\u image”:真,
“状态计数”:31,
“配置文件\背景\颜色”:“C0”,
“追随者数量”:3066,
“配置文件\图像\ url”:http://a2.twimg.com/profile_images/1285770264/PGP_normal.jpg", 
“列出的数量”:6,
“配置文件\背景\图像\ url”:http://a3.twimg.com/a/1301071706/images/themes/theme1/bg.png", 
“说明”:“,
“屏幕名称”:“研究生问题”,
“默认_配置文件”:true,
“已验证”:错误,
“时区”:空,
“配置文件\文本\颜色”:“333333”,
“is_translator”:错误,
“配置文件\侧边栏\填充\颜色”:“DDEEF6”,
“地点”:“,
“id_街”:“271572434”,
“默认_配置文件_图像”:false,
“配置文件\u背景\u平铺”:false,
“郎”:“恩”,
“朋友数”:21,
“受保护”:错误,
“收藏夹计数”:0,
“创建时间”:“2011年3月24日星期四19:45:44+0000”,
“配置文件链接颜色”:“0084B4”,
“姓名”:“研究生问题”,
“显示所有内联媒体”:false,
“跟踪请求发送”:空,
“已启用地理位置”:false,
“配置文件\侧边栏\边框\颜色”:“C0”,
“url”:空,
“id”:271572434,
“已启用贡献者”:false,
“following”:空,
“utc_偏移量”:空
}, 
“id”:54640519019642880,
“坐标”:空,
“geo”:空
}, 
“用户”:{
“通知”:空,
“profile\u use\u background\u image”:真,
“状态计数”:351,
“配置文件\背景\颜色”:“C0”,
“粉丝数”:48,
“配置文件\图像\ url”:http://a1.twimg.com/profile_images/455128973/gCsVUnofNqqyd6tdOGevROvko1_500_normal.jpg", 
“列出的计数”:0,
“配置文件\背景\图像\ url”:http://a3.twimg.com/a/1300479984/images/themes/theme1/bg.png", 
“描述”:“守望者在我的水域里干什么?”,
“屏幕名称”:“OldGREG85”,
“默认_配置文件”:true,
“已验证”:错误,
“时区”:“夏威夷”,
“配置文件\文本\颜色”:“333333”,
“is_translator”:错误,
“配置文件\侧边栏\填充\颜色”:“DDEEF6”,
“地点”:“德克萨斯州”,
“id_str”:“80177619”,
“默认_配置文件_图像”:false,
“配置文件\u背景\u平铺”:false,
“郎”:“恩”,
“朋友数”:81,
“受保护”:错误,
“收藏夹计数”:0,
“创建时间”:“2009年10月6日星期二01:13:17+0000”,
“配置文件链接颜色”:“0084B4”,
“名称”:“GG”,
“显示所有内联媒体”:false,
“跟踪请求发送”:空,
“已启用地理位置”:false,
“配置文件\侧边栏\边框\颜色”:“C0”,
“url”:空,
“id”:80177619,
“已启用贡献者”:false,
“following”:空,
“utc_偏移量”:-36000
}, 
“id”:54691802283900930,
“坐标”:空,
“geo”:空
} {
“文本”:“RT@PostgradeProblem:为NFL停摆做准备,我将在……期间花两倍的时间分析我的梦幻棒球队,
“截断”:正确,
“in_reply_to_user_id”:null,
“在对状态id的回复中”:null,
“偏爱”:错误,
“来源”:“,
“回复至屏幕名称”为空,
with open('fetched_tweets.json','a') as tf:
    json.dump(data, tf)
    tf.write("\n")
with open('fetched_tweets.json', 'r') as tf:
    for line in tf:
        data = json.loads(line)
        # save the `data` dictionary to BigQuery