python3生成json
程序员文章站
2022-07-03 12:06:31
...
import json
from collections import defaultdict
res=['253,219,272,219,272,237,253,237', '385,196,399,195,401,212,387,213', '138,186,160,188,157,218,135,216', '220,165,252,165,252,197,220,197', '314,160,333,160,333,188,314,188', '273,159,294,159,294,192,273,192', '191,148,205,148,205,164,191,164', '391,144,417,144,417,176,391,176', '283,150,288,150,288,153,283,153', '335,133,356,133,356,165,335,165', '381,114,397,114,397,131,381,131', '449,108,471,110,469,139,447,137', '214,111,227,111,227,128,214,128', '262,100,278,100,278,125,262,125', '323,97,345,97,345,116,323,116', '366,89,384,89,384,105,366,105', '108,84,128,84,128,113,108,113', '192,73,224,72,226,104,193,105', '242,74,263,75,262,103,241,102', '430,57,448,58,446,86,428,85', '288,50,309,52,307,79,286,78', '345,52,363,52,363,81,345,81', '181,40,199,40,199,56,181,56', '343,19,355,19,355,34,343,34']
body = defaultdict(list)
dicts = defaultdict(list)
for i in range(len(res)):
pos_dict = {
"id": i,
"boundingBox": res[i]
}
body['element'].append(pos_dict)
dicts = {"imagename": 'image_path', "body": body}
json_str = json.dumps(dicts, indent=4).encode('utf-8').decode("unicode-escape")
print(json_str)