【问题标题】:How can I access a children inside a json file如何访问 json 文件中的子项
【发布时间】:2018-12-30 15:30:53
【问题描述】:
                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 语句

【问题讨论】:

  • 你能展示你写的东西和你的json文件吗?
  • 很难想象你的 json 文件和 if 语句和整个代码结构是什么样子的。请在问题中写出来。
  • 仍然无法查看您的 json 文件。需要注意的一件事。迭代时不要删除您的数据。
  • 忽略[i-1]是错误的应该是[i]
  • 你的初始 i 值是多少?你把它设为 0 了吗?

标签: python json multidimensional-array


【解决方案1】:

嗯,访问儿童非常简单。

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

但离你的代码太远了,

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

给你一个字典,例如

{
     "name": "Samsung"
}

但是你正在将它与一个字符串进行比较:

str(item.text(0))

Dict==Str 始终为 False。所以代码永远不会进入你的 if 语句。

【讨论】:

    【解决方案2】:

    我认为在这里使用 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('小时', 无)

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-31
      • 2012-06-19
      • 2019-09-06
      • 2020-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-02
      相关资源
      最近更新 更多