python json load内层没有解析_Python Json load()返回字符串而不是字典?

本文介绍了如何在Python中解析包含复杂结构的JSON数据,包括访问内层字典和列表的方法,以及如何避免将JSON字符串误处理为普通字符串导致的问题。
摘要由CSDN通过智能技术生成

好的,首先你应该打印你的对象,以便你可以阅读它:

>>> from pprint import pprint

>>> output = [{'in_reply_to_status_id_str': None, 'in_reply_to_screen_name': None, 'retweeted': False, 'in_reply_to_status_id': None, 'contributors': None, 'favorite_count': 0, 'in_reply_to_user_id': None, 'coordinates': None, 'source': 'Twitter Web Client', 'geo': None, 'retweet_count': 0, 'text': 'Tweeting a url

http://t.co/QDVYv6bV90', 'created_at': 'Mon Sep 01 19:36:25 +0000 2014', 'entities': {'symbols': [], 'user_mentions': [], 'urls': [{'expanded_url': 'http://www.isthereanappthat.com', 'display_url': 'isthereanappthat.com', 'url': 'http://t.co/QDVYv6bV90', 'indices': [16, 38]}], 'hashtags': []}, 'id_str': '506526005943865344', 'in_reply_to_user_id_str': None, 'truncated': False, 'favorited': False, 'lang': 'en', 'possibly_sensitive': False, 'id': 506526005943865344, 'user': {'profile_text_color': '333333', 'time_zone': None, 'entities': {'description': {'urls': []}}, 'url': None, 'profile_background_image_url': 'http://abs.twimg.com/images/themes/theme1/bg.png', 'profile_background_image_url_https': 'https://abs.twimg.com/images/themes/theme1/bg.png', 'protected': False, 'default_profile_image': True, 'utc_offset': None, 'default_profile': True, 'screen_name': 'KickzWatch', 'follow_request_sent': False, 'following': False, 'profile_background_color': 'C0DEED', 'notifications': False, 'description': '', 'profile_sidebar_border_color': 'C0DEED', 'geo_enabled': False, 'verified': False, 'friends_count': 40, 'created_at': 'Mon Sep 01 16:29:18 +0000 2014', 'is_translator': False, 'profile_sidebar_fill_color': 'DDEEF6', 'statuses_count': 4, 'location': '', 'id_str': '2784389341', 'followers_count': 4, 'favourites_count': 0, 'contributors_enabled': False, 'is_translation_enabled': False, 'lang': 'en', 'profile_image_url': 'http://abs.twimg.com/sticky/default_profile_images/default_profile_6_normal.png', 'profile_image_url_https': 'https://abs.twimg.com/sticky/default_profile_images/default_profile_6_normal.png', 'id': 2784389341, 'profile_use_background_image': True, 'listed_count': 0, 'profile_background_tile': False, 'name': 'Maktub Destiny', 'profile_link_color': '0084B4'}, 'place': None}]

>>> pprint(output)

[{'contributors': None,

'coordinates': None,

'created_at': 'Mon Sep 01 19:36:25 +0000 2014',

'entities': {'hashtags': [],

'symbols': [],

'urls': [{'display_url': 'isthereanappthat.com',

'expanded_url': 'http://www.isthereanappthat.com',

'indices': [16, 38],

'url': 'http://t.co/QDVYv6bV90'}],

'user_mentions': []},

'favorite_count': 0,

'favorited': False,

'geo': None,

'id': 506526005943865344,

'id_str': '506526005943865344',

'in_reply_to_screen_name': None,

'in_reply_to_status_id': None,

'in_reply_to_status_id_str': None,

'in_reply_to_user_id': None,

'in_reply_to_user_id_str': None,

'lang': 'en',

'place': None,

'possibly_sensitive': False,

'retweet_count': 0,

'retweeted': False,

'source': 'Twitter Web Client',

'text': 'Tweeting a url

http://t.co/QDVYv6bV90',

'truncated': False,

'user': {'contributors_enabled': False,

'created_at': 'Mon Sep 01 16:29:18 +0000 2014',

'default_profile': True,

'default_profile_image': True,

'description': '',

'entities': {'description': {'urls': []}},

'favourites_count': 0,

'follow_request_sent': False,

'followers_count': 4,

'following': False,

'friends_count': 40,

'geo_enabled': False,

'id': 2784389341,

'id_str': '2784389341',

'is_translation_enabled': False,

'is_translator': False,

'lang': 'en',

'listed_count': 0,

'location': '',

'name': 'Maktub Destiny',

'notifications': False,

'profile_background_color': 'C0DEED',

'profile_background_image_url': 'http://abs.twimg.com/images/themes/theme1/bg.png',

'profile_background_image_url_https': 'https://abs.twimg.com/images/themes/theme1/bg.png',

'profile_background_tile': False,

'profile_image_url': 'http://abs.twimg.com/sticky/default_profile_images/default_profile_6_normal.png',

'profile_image_url_https': 'https://abs.twimg.com/sticky/default_profile_images/default_profile_6_normal.png',

'profile_link_color': '0084B4',

'profile_sidebar_border_color': 'C0DEED',

'profile_sidebar_fill_color': 'DDEEF6',

'profile_text_color': '333333',

'profile_use_background_image': True,

'protected': False,

'screen_name': 'KickzWatch',

'statuses_count': 4,

'time_zone': None,

'url': None,

'utc_offset': None,

'verified': False}}]

通过查看此信息,您可以看到输出是一个包含单个字典的列表.要访问它,您需要:

>>> first_elem = output[0]

您还将看到first_elem中的hashtags键包含在关键实体下的第二级dict中:

>>> entities = first_elem['entities']

>>> pprint(entities)

{'hashtags': [],

'symbols': [],

'urls': [{'display_url': 'isthereanappthat.com',

'expanded_url': 'http://www.isthereanappthat.com',

'indices': [16, 38],

'url': 'http://t.co/QDVYv6bV90'}],

'user_mentions': []}

现在您可以访问主题标签:

>>> entities['hashtags']

[]

这恰好是空列表.

要转换为JSON,请注意注释:

>>> import json

>>> # Make sure output is the list object not a string representing the object

>>> json_string = json.dumps(output)

>>> jason = json.loads(output)

>>> jason[0]['entities']['hashtags']

[]

我认为你的问题是你在json.dump之前输出了一个字符串,这意味着json.loads将返回一个字符串,而不是一个json对象.

而@ Dan的答案是正确的,这不是有效的JSON.然而它是一个有效的python dict,我假设你从Twitter使用python然后打印它.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值