Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 迭代JSON对象列表_Python_Json_Sorting_Iterator - Fatal编程技术网

Python 迭代JSON对象列表

Python 迭代JSON对象列表,python,json,sorting,iterator,Python,Json,Sorting,Iterator,我有一个JSON文件,其中包含如下列表: "locales": { "de": { "default": { "vegetables": [ "apple", "melon", "grape", "pear" ] } }, "fr": { "d

我有一个JSON文件,其中包含如下列表:

"locales": {
    "de": {
        "default": {
            "vegetables": [
                "apple", 
                "melon", 
                "grape", 
                "pear"
            ]
        }
    }, 
    "fr": {
        "default": {
            "vegetables": [
                "apple", 
                "melon", 
                "grape", 
                "pear"
            ]
        }
    }, 
    "nl": {
        "default": {
            "vegetables": [
                "apple", 
                "melon", 
                "grape", 
                "pear"
            ]
        }
    },
(...)
}
我想根据区域设置(de、fr、nl等)重复使用
区域设置
ad
蔬菜
。如何使用python实现这一点

我试过类似的东西

import json 

_MY_CUSTOM_ORDER_DE = [
    "pear",
    "grape",
    "apple",
    "melon"]

_MY_CUSTOM_ORDER_NL = [
    "melon",
    "pear",
    "apple",
    "grape"]

def updateJsonFile():
    jsonFile = open('vegetables.json', 'r') 
    data = json.load(jsonFile) 
    jsonFile.close() 

    for item in data["locales"]:
        if item == "de":
            item["default"]["vegetables"] = _MY_CUSTOM_ORDER_DE
        elif item == "nl":
            item["default"]["vegetables"] = _MY_CUSTOM_ORDER_NL
        else:
            ## do nothing ##

        with open('sortedvegetables.json', 'w') as outfile:
        json.dump(data, outfile, indent=4, sort_keys=True)

def main():
    updateJsonFile()

if __name__ == '__main__':
    main()
不幸的是,这不起作用,但在我执行脚本后,终端中出现了错误
sortedvegetables.json
看起来与蔬菜完全一样。json

以下是您的答案:

您的代码中出现了一些逻辑故障

1) 您试图将其作为列表进行比较

if item == ["de"]:
如果item==“de”,则应为:

2) 您试图更新为
项[“默认”][“蔬菜”]
,这是不正确的方式,应该适当地将其写入
数据[“地区”][“de”][“默认”][“蔬菜”]
数据[“地区”][“nl”][“默认”][“蔬菜”]

import json

_MY_CUSTOM_ORDER_DE = [
    "pear",
    "grape",
    "apple",
    "melon"]

_MY_CUSTOM_ORDER_NL = [
    "melon",
    "pear",
    "apple",
    "grape"]

def updateJsonFile():
    jsonFile = open('vegetables.json', 'r')
    data = json.load(jsonFile)
    jsonFile.close()

    for item in data["locales"]:
        print(item)
        if item == "de":
            print(item)
            data["locales"]["de"]["default"]["vegetables"] = _MY_CUSTOM_ORDER_DE
        elif item == "nl":
            print(item)
            data["locales"]["nl"]["default"]["vegetables"] = _MY_CUSTOM_ORDER_NL
        else:
            print("test")
            pass

        with open('sortedvegetables.json', 'w') as outfile:
            json.dump(data, outfile, indent=4, sort_keys=True)

def main():
    updateJsonFile()

if __name__ == '__main__':
    main()

您对JSON有一个根本性的误解。字典里没有固有的顺序;试图对条目进行排序是毫无意义的


早期版本中的Python会以解释器内部存储的顺序转储内部
dict
,而不管您对其进行了多少排序。使用最新的Python版本,您可以预期dict将按照您创建它的顺序返回。。。但最终,JSON语义仍然是一种表示形式,对于字典来说没有任何顺序。

你能说得更清楚吗?无法理解你到底在看什么?请给出一些明确的例子!还显示什么是“我的自定义订单”和“我的自定义订单”\n您在哪里调用
updateJsonFile
?我编辑并粘贴了完整的python脚本