path = r'path'
json_list = os.listdir(path)
for json_file in json_list:
if os.path.splitext(json_file)[-1] == '.json':
print(json_file)
with open(os.path.join(path,json_file),'r',encoding='utf-8') as load_f:
data = json.load(load_f)
shape = data ['shapes']
print(shape)
我要获取json指定的内容,下面是运行的结果,我不要最外层的方括号,请问如何实现?
[{'label': '1', 'points': [[100.95238095238096, 173.0], [189.04761904761904, 265.8571428571429]], 'group_id': None, 'shape_type': 'rectangle', 'flags': {}}, {'label': '3', 'points': [[421.19047619047626, 488.4761904761905], [581.9047619047619, 562.2857142857143]], 'group_id': None, 'shape_type': 'circle', 'flags': {}}, {'label': '2', 'points': [[798.5714285714287, 290.8571428571429], [827.1428571428571, 317.04761904761904]], 'group_id': None, 'shape_type': 'circle', 'flags': {}}, {'label': '2', 'points': [[753.3333333333335, 153.95238095238096], [852.1428571428571, 248.0]], 'group_id': None, 'shape_type': 'rectangle', 'flags': {}}, {'label': '2', 'points': [[798.5714285714287, 290.8571428571429], [827.1428571428571, 317.04761904761904]], 'group_id': None, 'shape_type': 'circle', 'flags': {}}]
如何去掉最外层方括号,实现获取的结果如下?
{'label': '1', 'points': [[100.95238095238096, 173.0], [189.04761904761904, 265.8571428571429]], 'group_id': None, 'shape_type': 'rectangle', 'flags': {}}, {'label': '3', 'points': [[421.19047619047626, 488.4761904761905], [581.9047619047619, 562.2857142857143]], 'group_id': None, 'shape_type': 'circle', 'flags': {}}, {'label': '2', 'points': [[798.5714285714287, 290.8571428571429], [827.1428571428571, 317.04761904761904]], 'group_id': None, 'shape_type': 'circle', 'flags': {}}, {'label': '2', 'points': [[753.3333333333335, 153.95238095238096], [852.1428571428571, 248.0]], 'group_id': None, 'shape_type': 'rectangle', 'flags': {}}, {'label': '2', 'points': [[798.5714285714287, 290.8571428571429], [827.1428571428571, 317.04761904761904]], 'group_id': None, 'shape_type': 'circle', 'flags': {}}
json 示例如下:
{
"version": "3.15.1",
"flags": {},
"shapes": [
{
"label": "2",
"points": [
[
753.3333333333335,
153.95238095238096
],
[
852.1428571428571,
248.0
]
],
"group_id": null,
"shape_type": "rectangle",
"flags": {}
},
{
"label": "2",
"points": [
[
753.3333333333335,
153.95238095238096
],
[
852.1428571428571,
248.0
]
],
"group_id": null,
"shape_type": "rectangle",
"flags": {}
},
{
"label": "2",
"points": [
[
753.3333333333335,
153.95238095238096
],
[
852.1428571428571,
248.0
]
],
"group_id": null,
"shape_type": "rectangle",
"flags": {}
}
],
"imagePath": "a.png",
"imageData": null,
"imageHeight": 1149,
"imageWidth": 912,
}