【问题标题】:In Python, how to replace a json subobject based on multiple attribute value settings?Python中,如何根据多个属性值设置替换一个json子对象?
【发布时间】:2021-07-14 06:46:43
【问题描述】:
[
{
    "builtin_name": "custom_template",
    "fields": [
        {
            "id": 10012,
            "field_type": "OBJECT_SET",
            "tooltip_text": "",
            "name_plural": "",
            "name_singular": "reference",
            "backref_name": "reference",
            "backref_tooltip_text": "",
            "allow_multiple": false,
            "allowed_otypes": [
                "schema",
                "table",
                "attribute",
                "user",
                "groupprofile",
                "groupprofile"
            ],
            "options": null,
            "builtin_name": null
        },
        {
            "id": 8,
            "field_type": "OBJECT_SET",
            "tooltip_text": null,
            "name_plural": "Stewards",
            "name_singular": "Steward",
            "backref_name": "Steward",
            "backref_tooltip_text": null,
            "allow_multiple": true,
            "allowed_otypes": ["user", "groupprofile", "groupprofile"],
            "options": null,
            "builtin_name": "steward"
        }
    ],
    "id": 16,
    "title": "Custom template"
},
{
    "builtin_name": "new_template",
    "fields": [
        {
            "id": 10011,
            "field_type": "PICKER",
            "tooltip_text": "",
            "name_plural": "",
            "name_singular": "status",
            "backref_name": "",
            "backref_tooltip_text": "",
            "allow_multiple": false,
            "allowed_otypes": null,
            "options": [
                {
                    "old_index": 0,
                    "article_id": null,
                    "tooltip_text": null,
                    "in_use": true,
                    "title": "Done"
                }
            ],
            "builtin_name": null
        }
    ],
    "id": 1899,
    "title": "New Template"
}

] 使用这个 JSON 对象,我想替换 fields 列表,其中 id=16 and title=Custom template。我已经生成了要用于替换的对象。

这是我尝试过的,但没有奏效:

i=0    
jdata_copy = jdata #this is so I can compare
for t in jdata:
    if(t["title"]=="Custom template" and t["id"]==16):            
        jdata_copy[i]["fields"] = repl_data
    i+=1 

我四处寻找一个简单的 json 替换,但没有找到。

感谢一些帮助。

谢谢!

【问题讨论】:

    标签: json python-3.x replace


    【解决方案1】:

    好的,假设 python 脚本与名为 test.json 的 json 文件位于同一目录中,这就是 json 的样子(与您的不一样,因为您的具有 nullNone 值某种原因,所以我不知道该怎么办):

    [
        {
            "builtin_name": "custom_template",
            "fields": [
                {
                    "id": 10012,
                    "field_type": "OBJECT_SET",
                    "tooltip_text": "",
                    "name_plural": "",
                    "name_singular": "reference",
                    "backref_name": "reference",
                    "backref_tooltip_text": "",
                    "allow_multiple": false,
                    "allowed_otypes": [
                        "schema",
                        "table",
                        "attribute",
                        "user",
                        "groupprofile",
                        "groupprofile"
                    ],
                    "options": null,
                    "builtin_name": null
                },
                {
                    "id": 8,
                    "field_type": "OBJECT_SET",
                    "tooltip_text": null,
                    "name_plural": "Stewards",
                    "name_singular": "Steward",
                    "backref_name": "Steward",
                    "backref_tooltip_text": null,
                    "allow_multiple": true,
                    "allowed_otypes": ["user", "groupprofile", "groupprofile"],
                    "options": null,
                    "builtin_name": "steward"
                }
            ],
            "id": 16,
            "title": "Custom template"
        },
        {
            "builtin_name": "new_template",
            "fields": [
                {
                    "id": 10011,
                    "field_type": "PICKER",
                    "tooltip_text": "",
                    "name_plural": "",
                    "name_singular": "status",
                    "backref_name": "",
                    "backref_tooltip_text": "",
                    "allow_multiple": false,
                    "allowed_otypes": null,
                    "options": [
                        {
                            "old_index": 0,
                            "article_id": null,
                            "tooltip_text": null,
                            "in_use": true,
                            "title": "Done"
                        }
                    ],
                    "builtin_name": null
                }
            ],
            "id": 1899,
            "title": "New Template"
        }
    ]
    

    然后,这段代码可以加载和更改fields

    import json
    
    
    with open("test.json") as json_file:
        jdata = json.load(json_file)
    
    jdata_copy = jdata
    

    对于 jdata 中的 t: 如果 t["id"] == 16 和 t["title"] == "自定义模板": pprint(jdata) 打印() t[“字段”] = [“你好”,“你好”] 打印() pprint(jdata)

    但是,如果您是这种情况,请告诉我,我不确定您的问题到底是什么。

    编辑

    因此,将代码更改为:

    import json
    from pprint import pprint
    
    
    with open("test.json") as json_file:
        jdata = json.load(json_file)
    
    jdata_copy = jdata
    
    for t in jdata:
        if t["id"] == 16 and t["title"] == "Custom template":
            pprint(jdata)
    
            t["fields"] = ["howdy", "hello"]
    
            pprint(jdata)
    

    有了这个,我可以看到第一个 print 语句将 json 作为一个 python 字典返回,第二个 print 语句确实显示 jdata 已经修改了正确字典的fields 部分。

    然后,我可以将字典导出回.json

    with open("test1.json", "w") as json_file:
        json.dump(jdata, json_file)
    

    这一切对我来说都很好,这里的哪个部分不适合你?对不起,如果我误解了。

    【讨论】:

    • 感谢您的回复。但这不起作用。我试过了。
    • 你能展示一下你是如何加载json的,json对象是什么等等,因为你上面提供的json数据没有多大意义
    • 我在上面的帖子中修复并更新了原件,这是我没有注意到的 python 工件。尽管如此,更换仍然没有什么区别。
    • 那是什么意思,在这之后你对jdata做什么?是不是又导出到 json 了?
    • 现在我已经在您的帮助下完成了它,真的没有问题,这是我的误解,我正在替换错误的东西。感谢您的帮助!
    猜你喜欢
    • 2021-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-21
    相关资源
    最近更新 更多