Python/Json:期望用双引号括起来的属性名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39491420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python/Json:Expecting property name enclosed in double quotes
提问by raeX
I've been trying to figure out a good way to load JSON objects in Python. I send this json data:
我一直在寻找一种在 Python 中加载 JSON 对象的好方法。我发送这个json数据:
{'http://example.org/about': {'http://purl.org/dc/terms/title': [{'type': 'literal', 'value': "Anna's Homepage"}]}}
to the backend where it will be received as a string then I used json.loads(data)
to parse it.
到后端,它将作为字符串接收,然后我用来json.loads(data)
解析它。
But each time I got the same exception :
但每次我都遇到相同的异常:
ValueError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
I googled it but nothing seems to work besides this solution json.loads(json.dumps(data))
which personally seems for me not that efficient since it accept any kind of data even the ones that are not in json format.
我用谷歌搜索了它,但除了这个解决方案之外似乎没有任何工作json.loads(json.dumps(data))
,我个人认为效率不高,因为它接受任何类型的数据,即使是那些不是 json 格式的数据。
Any suggestions will be much appreciated.
任何建议将不胜感激。
回答by ElmoVanKielmo
This:
这个:
{'http://example.org/about': {'http://purl.org/dc/terms/title': [{'type': 'literal', 'value': "Anna's Homepage"}]}}
is not JSON.
This:
不是 JSON。
这个:
{"http://example.org/about": {"http://purl.org/dc/terms/title": [{"type": "literal", "value": "Anna's Homepage"}]}}
is JSON.
是 JSON。
EDIT:
Some commenters suggested that the above is not enough.
JSON specification - RFC7159states that a string begins and ends with quotation mark. That is "
.
Single quoute '
has no semantic meaning in JSON and is allowed only inside a string.
编辑:
一些评论者认为以上是不够的。
JSON 规范 - RFC7159规定字符串以引号开始和结束。那就是"
。
单引号'
在 JSON 中没有语义意义,只允许在字符串内部使用。
回答by elig
as JSON only allows enclosing strings with double quotes you can manipulate the string like this:
由于 JSON 只允许用双引号括起字符串,因此您可以像这样操作字符串:
str = str.replace("\'", "\"")
This will replace all occurrences of single quote with double quote in the JSON string str
.
这将在 JSON 字符串中用双引号替换所有出现的单引号str
。
You can also use js-beautify
which is less strict:
您还可以使用js-beautify
哪个不那么严格:
$ pip install jsbeautifier
$ js-beautify file.js
回答by greentec
In my case, double quotes was not a problem.
就我而言,双引号不是问题。
Last comma gave me same error message.
最后一个逗号给了我同样的错误信息。
{'a':{'b':c,}}
^
To remove this comma, I wrote some simple code.
为了去掉这个逗号,我写了一些简单的代码。
import json
with open('a.json','r') as f:
s = f.read()
s = s.replace('\t','')
s = s.replace('\n','')
s = s.replace(',}','}')
s = s.replace(',]',']')
data = json.loads(s)
And this worked for me.
这对我有用。
回答by Daniel Roseman
Quite simply, that string is not valid JSON. As the error says, JSON documents need to use double quotes.
很简单,该字符串不是有效的 JSON。正如错误所说,JSON 文档需要使用双引号。
You need to fix the source of the data.
您需要修复数据的来源。
回答by alexbclay
JSON strings must use double quotes. The JSON python library enforces this so you are unable to load your string. Your data needs to look like this:
JSON 字符串必须使用双引号。JSON python 库强制执行此操作,因此您无法加载字符串。您的数据需要如下所示:
{"http://example.org/about": {"http://purl.org/dc/terms/title": [{"type": "literal", "value": "Anna's Homepage"}]}}
If that's not something you can do, you could use ast.literal_eval()
instead of json.loads()
如果这不是你可以做的,你可以使用ast.literal_eval()
代替json.loads()
回答by Yaron
I've checked your JSON data
我检查了你的 JSON 数据
{'http://example.org/about': {'http://purl.org/dc/terms/title': [{'type': 'literal', 'value': "Anna's Homepage"}]}}
in http://jsonlint.com/and the results were:
在http://jsonlint.com/ 中,结果是:
Error: Parse error on line 1:
{ 'http://example.org/
--^
Expecting 'STRING', '}', got 'undefined'
modifying it to the following string solve the JSON error:
将其修改为以下字符串可解决 JSON 错误:
{
"http://example.org/about": {
"http://purl.org/dc/terms/title": [{
"type": "literal",
"value": "Anna's Homepage"
}]
}
}
回答by Pavel Gurkov
As it clearly says in error, names should be enclosed in double quotes instead of single quotes. The string you pass is just not a valid JSON. It should look like
正如它在错误中明确指出的那样,名称应该用双引号而不是单引号括起来。您传递的字符串不是有效的 JSON。它应该看起来像
{"http://example.org/about": {"http://purl.org/dc/terms/title": [{"type": "literal", "value": "Anna's Homepage"}]}}
回答by Hamed
I used this method and managed to get the desired output. my script
我使用了这种方法并设法获得了所需的输出。我的剧本
x = "{'inner-temperature': 31.73, 'outer-temperature': 28.38, 'keys-value': 0}"
x = x.replace("'", '"')
j = json.loads(x)
print(j['keys-value'])
output
输出
>>> 0
回答by rohit9786
with open('input.json','r') as f:
s = f.read()
s = s.replace('\'','\"')
data = json.loads(s)
This worked perfectly well for me. Thanks.
这对我来说非常有效。谢谢。
回答by Amit Ghosh
x = x.replace("'", '"')
j = json.loads(x)
Although this is the correct solution, but it may lead to quite a headache if there a JSON like this -
虽然这是正确的解决方案,但如果有这样的 JSON 可能会导致相当头痛 -
{'status': 'success', 'data': {'equity': {'enabled': True, 'net': 66706.14510000008, 'available': {'adhoc_margin': 0, 'cash': 1277252.56, 'opening_balance': 1277252.56, 'live_balance': 66706.14510000008, 'collateral': 249823.93, 'intraday_payin': 15000}, 'utilised': {'debits': 1475370.3449, 'exposure': 607729.3129, 'm2m_realised': 0, 'm2m_unrealised': -9033, 'option_premium': 0, 'payout': 0, 'span': 858608.032, 'holding_sales': 0, 'turnover': 0, 'liquid_collateral': 0, 'stock_collateral': 249823.93}}, 'commodity': {'enabled': True, 'net': 0, 'available': {'adhoc_margin': 0, 'cash': 0, 'opening_balance': 0, 'live_balance': 0, 'collateral': 0, 'intraday_payin': 0}, 'utilised': {'debits': 0, 'exposure': 0, 'm2m_realised': 0, 'm2m_unrealised': 0, 'option_premium': 0, 'payout': 0, 'span': 0, 'holding_sales': 0, 'turnover': 0, 'liquid_collateral': 0, 'stock_collateral': 0}}}}
Noticed that "True"value? Use this to make things are double checked for Booleans. This will cover those cases -
注意到“真”值了吗?使用它可以对布尔值进行双重检查。这将涵盖这些情况 -
x = x.replace("'", '"').replace("True", '"True"').replace("False", '"False"').replace("null", '"null"')
j = json.loads(x)
Also, make sure you do not make
另外,请确保您不
x = json.loads(x)
It has to be another variable.
它必须是另一个变量。