jimkongho 2022-05-03 14:13 采纳率: 80%
浏览 88
已结题

json获取指定Key和Value的问题

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,
}

  • 写回答

2条回答 默认 最新

  • 你好!机器人 2022-05-03 14:18
    关注

    获取数组的第一个
    shapes[0]

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 5月3日
  • 已采纳回答 5月3日
  • 修改了问题 5月3日
  • 修改了问题 5月3日
  • 展开全部

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探