简体   繁体   English

json.JSONDecoder()。decode()无法正常工作

[英]json.JSONDecoder().decode() can not work well

code is simple, but it can not work. 代码很简单,但是无法正常工作。 I don't know the problem 我不知道问题

import json

json_data = '{text: \"tl4ZCTPzQD0k|rEuPwudrAfgBD3nxFIsSbb4qMoYWA=\", key: \"MPm0ZIlk9|ADco64gjkJz2NwLm6SWHvW\"}'
my_data = json.JSONDecoder().decode(json_data)
print my_data

throw exption behinde: 抛弃经验:

Traceback (most recent call last):
  File "D:\Python27\project\demo\digSeo.py", line 4, in <module>
    my_data = json.JSONDecoder().decode(json_data)
  File "D:\Python27\lib\json\decoder.py", line 366, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "D:\Python27\lib\json\decoder.py", line 382, in raw_decode
    obj, end = self.scan_once(s, idx)
ValueError: Expecting property name: line 1 column 2 (char 1)

Your json_data is not valid JSON. 您的json_data是无效的JSON。

In JSON, property names need to be in double quotes ( " ). Also, the double quotes terminating the string values don't need to be ecaped since you're already using single quotes ( ' ) for the string. 在JSON中,属性名称必须使用双引号( " )。此外,由于已在字符串中使用单引号( ' ),因此不需要对终止字符串值的双引号进行转义。

Example: 例:

json_data = '{"text": "tl4ZCTPzQD0k|rEuPwudrAfgBD3nxFIsSbb4qMoYWA=", "key": "MPm0ZIlk9|ADco64gjkJz2NwLm6SWHvW"}'

The json module in Python standard library can work well, that's what a lot of people are using for their applications. Python标准库中的json模块可以很好地工作,这就是很多人在其应用程序中使用的模块。

However these few lines of code that use this module have a small issue. 但是,使用此模块的这几行代码有一个小问题。 The problem is that your sample data is not a valid JSON. 问题在于您的样本数据不是有效的JSON。 The keys (text and key) should be quoted like this: 键(文本和键)应这样引用:

json_data = '{"text": \"tl4ZCTPzQD0k|rEuPwudrAfgBD3nxFIsSbb4qMoYWA=\", "key": \"MPm0ZIlk9|ADco64gjkJz2NwLm6SWHvW\"}'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM