JSON 使用 python json 模块的解码器错误
JSON decoder error using python json module
我有这个简单的子数据结构:
{
"hostname": "CORESW01",
"vendor": "Cisco",
"isAlive": True,
"uptime": 123456,
"users": {
"admin": 15,
"storage": 10,
},
"vlans": [
{
"vlan_name": "VLAN30",
"vlan_id": 30
},
{
"vlan_name": "VLAN20",
"vlan_id": 20
}
]
}
我正在尝试使用以下代码解析:
import json
with open("json-example.json") as f:
data = f.read()
json_dict = json.loads(data)
print("Now printing each item in this document and the type it contains")
for k, v in json_dict.items():
print("-- The key {0} contains a {1} value.".format(str(k), str(type(v))))
执行代码时出现以下错误:
Traceback (most recent call last):
File "example-json.py", line 11, in <module>
json_dict = json.loads(data)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 4 column 14 (char 62)
谁能帮我诊断一下这个故障。我尝试使用 pub 单步执行代码,但我不清楚出现异常的原因。
JSON 中的布尔文字是小写 true
和 false
,但您使用的是 Python 大写文字:
...
"isAlive": True,
...
改用正确的小写拼写:
...
"isAlive": true,
...
您的其中一个对象中还有一个尾随逗号:
...
"users": {
"admin": 15,
"storage": 10,
},
....
JSON 不允许尾随逗号,您必须将其删除:
...
"users": {
"admin": 15,
"storage": 10
},
....
您的完整 JSON 应如下所示:
{
"hostname": "CORESW01",
"vendor": "Cisco",
"isAlive": true,
"uptime": 123456,
"users": {
"admin": 15,
"storage": 10
},
"vlans": [
{
"vlan_name": "VLAN30",
"vlan_id": 30
},
{
"vlan_name": "VLAN20",
"vlan_id": 20
}
]
}
可以正常加载:
json.loads('''{
"hostname": "CORESW01",
"vendor": "Cisco",
"isAlive": true,
"uptime": 123456,
"users": {
"admin": 15,
"storage": 10
},
"vlans": [
{
"vlan_name": "VLAN30",
"vlan_id": 30
},
{
"vlan_name": "VLAN20",
"vlan_id": 20
}
]
}''')
# {'hostname': 'CORESW01', 'vendor': 'Cisco', 'isAlive': True, 'uptime': 123456, 'users': {'admin': 15, 'storage': 10}, 'vlans': [{'vlan_name': 'VLAN30', 'vlan_id': 30}, {'vlan_name': 'VLAN20', 'vlan_id': 20}]}
我有这个简单的子数据结构:
{
"hostname": "CORESW01",
"vendor": "Cisco",
"isAlive": True,
"uptime": 123456,
"users": {
"admin": 15,
"storage": 10,
},
"vlans": [
{
"vlan_name": "VLAN30",
"vlan_id": 30
},
{
"vlan_name": "VLAN20",
"vlan_id": 20
}
]
}
我正在尝试使用以下代码解析:
import json
with open("json-example.json") as f:
data = f.read()
json_dict = json.loads(data)
print("Now printing each item in this document and the type it contains")
for k, v in json_dict.items():
print("-- The key {0} contains a {1} value.".format(str(k), str(type(v))))
执行代码时出现以下错误:
Traceback (most recent call last):
File "example-json.py", line 11, in <module>
json_dict = json.loads(data)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 4 column 14 (char 62)
谁能帮我诊断一下这个故障。我尝试使用 pub 单步执行代码,但我不清楚出现异常的原因。
JSON 中的布尔文字是小写 true
和 false
,但您使用的是 Python 大写文字:
...
"isAlive": True,
...
改用正确的小写拼写:
...
"isAlive": true,
...
您的其中一个对象中还有一个尾随逗号:
...
"users": {
"admin": 15,
"storage": 10,
},
....
JSON 不允许尾随逗号,您必须将其删除:
...
"users": {
"admin": 15,
"storage": 10
},
....
您的完整 JSON 应如下所示:
{
"hostname": "CORESW01",
"vendor": "Cisco",
"isAlive": true,
"uptime": 123456,
"users": {
"admin": 15,
"storage": 10
},
"vlans": [
{
"vlan_name": "VLAN30",
"vlan_id": 30
},
{
"vlan_name": "VLAN20",
"vlan_id": 20
}
]
}
可以正常加载:
json.loads('''{
"hostname": "CORESW01",
"vendor": "Cisco",
"isAlive": true,
"uptime": 123456,
"users": {
"admin": 15,
"storage": 10
},
"vlans": [
{
"vlan_name": "VLAN30",
"vlan_id": 30
},
{
"vlan_name": "VLAN20",
"vlan_id": 20
}
]
}''')
# {'hostname': 'CORESW01', 'vendor': 'Cisco', 'isAlive': True, 'uptime': 123456, 'users': {'admin': 15, 'storage': 10}, 'vlans': [{'vlan_name': 'VLAN30', 'vlan_id': 30}, {'vlan_name': 'VLAN20', 'vlan_id': 20}]}