我正在尝试使用json.loads()函数将sttr数据类型转换为python中的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)
答案 0 :(得分:0)
在注释中,您给出了要作为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