如何访问json文件中的子级

时间:2018-07-23 08:20:40

标签: python json multidimensional-array

                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

JSON:
"system": [
        {
            "title": "AirConditioning",
            "children": [
                {
                    "name": "Samsung"
                },
                {
                    "name": "Daikin"
                },

for element in data["system"]:
                #print(element)
                
                for index_in, nested_node in enumerate(data["system"][i-1]):
                    print(data["system"][i-1]["children"][index_in])
                    if nested_node["system"][i-1]["children"][index_in] == str(item.text(0)):
                        del data["system"][i-1]["children"][index_in]
                        #del element[index_in]
                    
                i = i + 1
            print(json.dumps(data, indent=4))
            with open("systems_list.json", "w", encoding="utf8") as json_file:
                json.dump(data, json_file, indent=4)

我正在尝试访问json文件中的子项,并基于if语句删除它,但看来我的程序从未输入if语句

2 个答案:

答案 0 :(得分:0)

嗯,访问儿童非常简单。

PS D:\dartstudy\second> flutter doctor -v
[√] Flutter (Channel dev, v0.5.7, on Microsoft Windows [Version 6.1.7601], locale zh-CN)
    • Flutter version 0.5.7 at E:\flutter
    • Framework revision 66091f9696 (2 weeks ago), 2018-07-09 12:52:41 -0700
    • Engine revision 6fe748490d
    • Dart version 2.0.0-dev.63.0.flutter-4c9689c1d2

[√] Android toolchain - develop for Android devices (Android SDK 28.0.1)
    • Android SDK at f:\Android\Sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-28, build-tools 28.0.1
    • ANDROID_HOME = f:\Android\Sdk
    • Java binary at: E:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1024-b02)
    • All Android licenses accepted.

[√] Android Studio (version 3.1)
    • Android Studio at E:\Program Files\Android\Android Studio
    • Flutter plugin version 26.0.1
    • Dart plugin version 173.4700
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1024-b02)

[√] VS Code, 64-bit edition (version 1.25.1)
    • VS Code at C:\Program Files\Microsoft VS Code
    • Flutter extension version 2.16.0

[!] Connected devices
    ! No devices available

! Doctor found issues in 1 category.

但是到目前为止,您的代码

data["system"][index_1]["children"][index_2]["name"] will bring you there.

为您带来一个字典,例如

nested_node["children"][index_in]['name']

但是您正在将其与字符串进行比较:

{
     "name": "Samsung"
}

str(item.text(0)) 始终为False。因此,代码永远不会进入您的if语句。

答案 1 :(得分:0)

我认为在这里使用i有点多余,因为无论如何您都在使用for循环。

为什么不这么做:

#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

对于数据中的元素:     element.pop('hours',None)

使用open(“ systems_list.json”,“ w”,encoding =“ utf8”)作为data_file:     json.dump(data,json_file,indent = 4)#保存新文件