json.loads() 给出"ValueError: Expecting property name:"



我正在尝试使用json.loads((函数将python中的sttr数据类型转换为dict。但我得到的错误是:

File "/usr/local/lib/python2.7/json/__init__.py", line 339, in loads
return _default_decoder.decode(s)
File "/usr/l`enter code here`ocal/lib/python2.7/json/decoder.py", line 364, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/local/lib/python2.7/json/decoder.py", line 380, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Expecting property name: line 1 column 2 (char 1)

在注释中,您给出了以下尝试加载为JSON的文本示例:

[
{
'entity_class': 'Hardware Entities (AHV)',
'status': 'recommended',
'uuid': 'de74178a-cbc7-4c69-ae2f-9e7042bf8e98', 
'zprotobuf': 'eNolzD1LAzEYB/DFScHBRXAKoUMr5khiLi/dCgpdCg7qIiLPJU/KQS4nyeEL6ne3p+vv/3K8CGiUMBaY77xhymvHAGVkDg1XsosWnT1bcI42tqZlwbWOqWAis5oD08J3VinOOwMXp', 
'version': '2.4-1542668003', 
'dependencies': '[{"entity_class": "Dell Update Manager", "version": "1.8-0.112", "exact": "false", "entity_model": "PT Agent on AHV (el6)"}]', 
'entity_uuid': '00e8f575-d959-4d7f-860a-61cb84400b7a', 
'order': 4,
}
]

JSON应该使用双引号,而不是单引号。上面看起来像是原生python数据结构语法。以下是上面从原生python转储到JSON的内容。

import json
native_python_data = [
{
'entity_class': 'Hardware Entities (AHV)',
'status': 'recommended',
'uuid': 'de74178a-cbc7-4c69-ae2f-9e7042bf8e98', 
'zprotobuf': 'eNolzD1LAzEYB/DFScHBRXAKoUMr5khiLi/dCgpdCg7qIiLPJU/KQS4nyeEL6ne3p+vv/3K8CGiUMBaY77xhymvHAGVkDg1XsosWnT1bcI42tqZlwbWOqWAis5oD08J3VinOOwMXp', 
'version': '2.4-1542668003', 
'dependencies': [
{
"entity_class": "Dell Update Manager", 
"version": "1.8-0.112", 
"exact": "false", 
"entity_model": "PT Agent on AHV (el6)"
}
], 
'entity_uuid': '00e8f575-d959-4d7f-860a-61cb84400b7a', 
'order': 4,
}
]
json_string_with_escaping = json.dumps(js, indent=4)
print json_string_with_escaping

相关内容

最新更新