我有一個人臉檢測代碼。我想要的是以特定的順序將元數據存儲在json文件中。基本上我想寫出圖像中存在多少人臉和圖像的位置。我的代碼如下:Python中嵌套的json
data =[]
data.append({'number_of_faces':len(faces)})
nested_data = []
for (x,y,w,h) in faces:
nested_data.append({'face_x': x, 'face_y': y, 'face_h': h, 'face_w': w})
data.append(nested_data)
with open(json_path+folder+'/'+file_name, "w") as outfile:
json.dump(data, outfile)
的輸出,例如:
[
{
"number_of_faces": 1
},
[
{
"face_h": 38,
"face_w": 38,
"face_x": 74,
"face_y": 45
}
]
]
不過,我想創建一個嵌套的JSON。因此我想在number_of_faces
對象之後嵌套number_of_faces
{}中的所有face_location
。這怎麼可能呢?
這不是真的關於JSON的問題,而是關於字典和列表。 'json'庫將會把你傳遞給它的結構序列化。 – 2014-09-26 08:21:20
是如何有效地將列表傳遞給json。 – 2014-09-26 08:23:10