Python 如何访问json文件中的子级

Python 如何访问json文件中的子级,python,json,multidimensional-array,Python,Json,Multidimensional Array,JSON: “系统”:[ { “标题”:“空调”, “儿童”:[ { “名称”:“三星” }, { “姓名”:“大金” },那么,访问子对象是非常简单的 i=0 for element in data["system"]: #print(element) for index_in, nested_node in enumerate(data["system"][i]["chi

JSON:
“系统”:[
{
“标题”:“空调”,
“儿童”:[
{
“名称”:“三星”
},
{
“姓名”:“大金”

},
那么,访问子对象是非常简单的

                i=0
            for element in data["system"]:
                #print(element)

                for index_in, nested_node in enumerate(data["system"][i]["children"]):
                    print(data["system"][i]["children"][index_in]["name"])
                    print(str(item.text(0)))
                    print(nested_node['name'])

                    if nested_node["name"] == str(item.text(0)):
                        print("Done")
                        del nested_node["system"][i]["children"][index_in]["name"]
                        #del element[index_in]

            i = i + 1
但到目前为止,你的代码

data["system"][index_1]["children"][index_2]["name"] will bring you there.
给你带来了一个dict,例如

nested_node["children"][index_in]['name']
但您将其与字符串进行比较:

{
     "name": "Samsung"
}

Dict==Str
始终为False。因此代码永远不会进入if语句。

我认为在这里使用I有点多余,因为您使用的是for循环

为什么不:

str(item.text(0))
对于数据中的元素: 元素.pop('hours',无)

打开(“systems\u list.json”,“w”,encoding=“utf8”)作为数据文件:
dump(数据,json_文件,缩进=4)#保存新文件

你能展示你所写的内容和你的json文件吗?很难想象你的json文件、if语句和整个代码结构是什么样子。请写下问题。仍然无法查看你的json文件。需要注意的一点是,在迭代过程中不要删除数据。忽略[i-1]这是一个错误,它应该是[i]初始i值是多少?您是否将其设置为0?
#open file
with open("systems_list.json", "r", encoding="utf8") as json_file:
            nested_json json.load(json_file)

# let's assume this is what it looks like inside nested_json: 
{"parent1" : 
    {"child 1": "sample1", "child 2": "sample2"} 
{"parent2" :
    {"child 1": "sample1", "child 2": "sample2"} 
}

for element in nested_json:
    if element["child 1"] == sample1:
        element.pop('hours', None) #this will erase the child based on value