如何用python从json对象中删除一个数组?

问题描述 投票:0回答:1

这是我的json文件的内容,我正在建立一个python应用程序来处理这个json文件,我需要删除一个数组。

{
  "tabID": [
    {
      "dat": [1, "q"],
      "opt": []
    }
  ]
}

我正在建立一个python应用程序来处理这个json文件 我需要删除掉一个数组 "opt":[].

我试过下一个代码

data['tabID'][0].remove('data') 

但它不工作。

你能给我一些建议吗?谢谢。

python json
1个回答
1
投票

把它做成一个dict,然后弹出键。

修正了你的json中的一个错误。工作示例在 https:/repl.itreplsFrighteningPungentEquipment。 或作为实际代码。

import json

the_json_string = '{"tabID":[{"dat":[1, "q"],"opt":[] }]}'

obj = json.loads(the_json_string)
obj['tabID'][0].pop('opt')

print(json.dumps(obj))
© www.soinside.com 2019 - 2024. All rights reserved.