简体   繁体   English

如何在 JSON 文件中进行更改并稍后在 Python 或 JS 中保存?

[英]How can I make changes in the JSON file and save it later in Python or JS?

I have one JSON file and I want to assign the key (food_image) value in it to index value in a loop.. for example food_image0, food_image1 .. food_image{loop index}我有一个 JSON 文件,我想将其中的键 (food_image) 值分配给循环中的索引值。例如 food_image0, food_image1 .. food_image{loop index}

Then i want to save this JSON file as JSON file.然后我想将此 JSON 文件保存为 JSON 文件。 .All values ​​with food_image key will be prefixed with food_image and will be rewritten by adding the index suffix in the loop. .所有带有food_image键的值都会以food_image为前缀,并通过在循环中添加索引后缀来重写。 It's all I want这就是我想要的

JSON JSON

{
    "DATABASE":
    [
        {
            "food_id": 0,
            "food_name": "Food Name ABC", 
            "food_image": "imagesite/abc.jpg", // food_image_0
            "food_kcal": "32",
            "units":
            [
                {
                    "amount": "15.0000",
                    "calory": "32.4877372383",
                },
                {
                    "amount": "110.0000",
                    "calory": "238.243406414",
                }
            ]
        },
        {
            "food_id": 1,
            "food_name": "Food Name XYZ", 
            "food_image": "imagesite/xyz.jpg", // food_image_1
            "food_kcal": "32",
            "units":
            [
                {
                    "amount": "15.0000",
                    "calory": "32.4877372383",
                },
                {
                    "amount": "110.0000",
                    "calory": "238.243406414",
                }
            ]
        }
        
    ]
}

Python Python

import json
with open('json_file.json',encoding="utf8") as myfile:
    data=myfile.read()

obj = json.loads(data)
for idx, i in enumerate(obj['DATABASE']):
    print(i["food_image"])


RESULT: JSON结果:JSON

{
    "DATABASE":
    [
        {
            "food_id": 0,
            "food_name": "Food Name ABC", 
            "food_image": "food_image_0.jpg", 
            "food_kcal": "32",
            "units":
            [
                {
                    "amount": "15.0000",
                    "calory": "32.4877372383",
                },
                {
                    "amount": "110.0000",
                    "calory": "238.243406414",
                }
            ]
        },
        {
            "food_id": 1,
            "food_name": "Food Name XYZ", 
            "food_image": "food_image_1.jpg"
            "food_kcal": "32",
            "units":
            [
                {
                    "amount": "15.0000",
                    "calory": "32.4877372383",
                },
                {
                    "amount": "110.0000",
                    "calory": "238.243406414",
                }
            ]
        }
        
    ]
}

Once you read your JSON file into a Python object, mutate the object, then write it back to a file.将 JSON 文件读入 Python 对象后,对对象进行变异,然后将其写回文件。

In the example below, I create a separate file instead of overwriting the original (which I would recommend you do too in case of bugs should you do other mutations).在下面的示例中,我创建了一个单独的文件而不是覆盖原始文件(如果您进行其他突变,我建议您也这样做,以防出现错误)。

import json
import os

source = 'json_file.json'
dest = 'updated.json'

with open(source) as f:
    content = json.load(f)

for i, entry in enumerate(content['DATABASE']):
    _, extension = os.path.splitext(entry['food_image'])
    entry['food_image'] = f'food_image_{i}{extension}'

with open(dest, 'w+') as f:
    json.dump(content, f)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM