鍍金池/ 問答/Java  Python/ python json 解析問題

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
回答
編輯回答
爛人

似乎無需用json再次解析, 直接將數(shù)據(jù)賦予一個字典變量, 即可使用.

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
2017年10月6日 20:01
編輯回答
命多硬

因為你現(xiàn)在的字符串是一個非標(biāo)準(zhǔn)的json字符串,用json.loads()不能解析None,最簡單的辦法使用eval


d = eval("你的json字符串")
2017年8月15日 18:38