python json 解析问题

{'forward_from_chat': None, 'migrate_to_chat_id': None, 'invoice': None, 'text': u'1', 'sticker': None, 'from_user': {'username': u'bamboo_lee', 'first_name': u'bamboo', 'last_name': u'lee', 'is_bot': False, 'language_code': u'en', 'id': 471214291}, 'caption_entities': None, 'delete_chat_photo': None, 'migrate_from_chat_id': None, 'new_chat_members': None, 'video': None, 'left_chat_member': None, 'chat': {'username': None, 'first_name': None, 'last_name': None, 'description': None, 'title': u'123', 'photo': None, 'pinned_message': None, 'sticker_set_name': None, 'all_members_are_administrators': True, 'invite_link': None, 'type': u'group', 'id': -264835065, 'can_set_sticker_set': None}, 'group_chat_created': None, 'new_chat_photo': None, 'forward_date': None, 'entities': None, 'location': None, 'photo': None, 'author_signature': None, 'document': None, 'forward_from': None, 'supergroup_chat_created': None, 'edit_date': None, 'pinned_message': None, 'content_type': 'text', 'successful_payment': None, 'date': 1514370687, 'new_chat_member': None, 'voice': None, 'reply_to_message': None, 'venue': None, 'message_id': 159, 'caption': None, 'contact': None, 'channel_chat_created': None, 'video_note': None, 'audio': None, 'new_chat_title': None}

这不是json吗?为什么解析错误

<type 'instance'>
2017-12-27 10:31:27,428 (util.py:64 WorkerThread1) ERROR - TeleBot: "TypeError occurred, args=('expected string or buffer',)
Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/telebot/util.py", line 58, in run
    task(*args, **kwargs)
  File "/tmp/pycharm_project_812/main.py", line 19, in handle_messages
    data = json.loads(message)
  File "/usr/lib64/python2.7/json/__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "/usr/lib64/python2.7/json/decoder.py", line 366, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
TypeError: expected string or buffer
"
Traceback (most recent call last):
  File "/tmp/pycharm_project_812/main.py", line 30, in <module>
    bot.polling()
  File "/usr/lib/python2.7/site-packages/telebot/__init__.py", line 264, in polling
    self.__threaded_polling(none_stop, interval, timeout)
  File "/usr/lib/python2.7/site-packages/telebot/__init__.py", line 288, in __threaded_polling
    self.worker_pool.raise_exceptions()
  File "/usr/lib/python2.7/site-packages/telebot/util.py", line 107, in raise_exceptions
    six.reraise(self.exc_info[0], self.exc_info[1], self.exc_info[2])
  File "/usr/lib/python2.7/site-packages/telebot/util.py", line 58, in run
    task(*args, **kwargs)
  File "/tmp/pycharm_project_812/main.py", line 19, in handle_messages
    data = json.loads(message)
  File "/usr/lib64/python2.7/json/__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "/usr/lib64/python2.7/json/decoder.py", line 366, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
TypeError: expected string or buffer
回复
阅读 3.6k
2 个回答

因为你现在的字符串是一个非标准的json字符串,用json.loads()不能解析None,最简单的办法使用eval


d = eval("你的json字符串")

似乎无需用json再次解析, 直接将数据赋予一个字典变量, 即可使用.

data={'forward_from_chat': None, 'migrate_to_chat_id': None, 'invoice': None, 'text': u'1', 'sticker': None, 'from_user': {'username': u'bamboo_lee', 'first_name': u'bamboo', 'last_name': u'lee', 'is_bot': False, 'language_code': u'en', 'id': 471214291}, 'caption_entities': None, 'delete_chat_photo': None, 'migrate_from_chat_id': None, 'new_chat_members': None, 'video': None, 'left_chat_member': None, 'chat': {'username': None, 'first_name': None, 'last_name': None, 'description': None, 'title': u'123', 'photo': None, 'pinned_message': None, 'sticker_set_name': None, 'all_members_are_administrators': True, 'invite_link': None, 'type': u'group', 'id': -264835065, 'can_set_sticker_set': None}, 'group_chat_created': None, 'new_chat_photo': None, 'forward_date': None, 'entities': None, 'location': None, 'photo': None, 'author_signature': None, 'document': None, 'forward_from': None, 'supergroup_chat_created': None, 'edit_date': None, 'pinned_message': None, 'content_type': 'text', 'successful_payment': None, 'date': 1514370687, 'new_chat_member': None, 'voice': None, 'reply_to_message': None, 'venue': None, 'message_id': 159, 'caption': None, 'contact': None, 'channel_chat_created': None, 'video_note': None, 'audio': None, 'new_chat_title': None}

print data['from_user']['is_bot']

output

False
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏