我有一个JSON字符串作为
{
"zone1" :
[
{
"detected_people" :
{
"coordinates" :
[
[
22,
3
],
[
21,
6
],
[
16,
21
]
]
},
"gridOrigin" :
[
28.5,
20.600000000000001
],
"gridScale" : 0.5,
"gridSizeX" : 50,
"gridSizeY" : 50,
"stamp" : "2020-08-28 22:23:05",
"zoneID" : "shop1"
}
]
}
当我使用json.dumps转换为JSON对象时
info=data.decode("utf-8")
print(info)
json_obj = json.dumps(info)
print(type(json_obj))
print(json_obj)
我有很多\t
,类型仍然是<class 'str'>
"{\n\t\"zone1\" : \n\t[\n\t\t{\n\t\t\t\"detected_people\" : \n\t\t\t{\n\t\t\t\t\"coordinates\" : \n\t\t\t\t[\n\t\t\t\t\t[\n\t\t\t\t\t\t22,\n\t\t\t\t\t\t3\n\t\t\t\t\t],\n\t\t\t\t\t[\n\t\t\t\t\t\t21,\n\t\t\t\t\t\t6\n\t\t\t\t\t],\n\t\t\t\t\t[\n\t\t\t\t\t\t16,\n\t\t\t\t\t\t21\n\t\t\t\t\t]\n\t\t\t\t]\n\t\t\t},\n\t\t\t\"gridOrigin\" : \n\t\t\t[\n\t\t\t\t28.5,\n\t\t\t\t20.600000000000001\n\t\t\t],\n\t\t\t\"gridScale\" : 0.5,\n\t\t\t\"gridSizeX\" : 50,\n\t\t\t\"gridSizeY\" : 50,\n\t\t\t\"stamp\" : \"2020-08-28 22:23:05\",\n\t\t\t\"zoneID\" : \"shop1\"\n\t\t}\n\t]\n}"
如何将JSON字符串info
转换为JSON对象?
答案 0 :(得分:0)
使用json.loads
代替json.dumps
:
info=data.decode("utf-8")
print(info)
json_obj = json.loads(info)
print(type(json_obj))
print(json_obj)
输出
{
"zone1" :
[
{
"detected_people" :
...........
"zoneID" : "shop1"
}
]
}
<class 'dict'>
{'zone1': [{'detected_people': {'coordinates': [[22, 3], [21, 6], [16, 21]]}, 'gridOrigin': [28.5, 20.6], 'gridScale': 0.5, 'gridSizeX': 50, 'gridSizeY': 50, 'stamp': '2020-08-28 22:23:05', 'zoneID': 'shop1'}]}