Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.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_Recursion_Pymongo - Fatal编程技术网

递归python JSON读取器

递归python JSON读取器,python,json,recursion,pymongo,Python,Json,Recursion,Pymongo,我在尝试使用递归函数时遇到了问题,因为我可以使用多个嵌套的dict和列表(有时在其他嵌套的dict和列表中嵌套dict和列表)转换json文件 基本上,我正在尝试以简单的OrderedDict组织一个JSON文件,以便将其传递到MongoDB数据库 下面是递归函数: def JSON_Treating(self,json_data): headers = [] data = [] for key, value in json_data.iteritems():

我在尝试使用递归函数时遇到了问题,因为我可以使用多个嵌套的dict和列表(有时在其他嵌套的dict和列表中嵌套dict和列表)转换json文件

基本上,我正在尝试以简单的OrderedDict组织一个JSON文件,以便将其传递到MongoDB数据库

下面是递归函数:

def JSON_Treating(self,json_data):

    headers = []
    data = []
    for key, value in json_data.iteritems():
        headers.append(key)
        try:
            if isinstance(value, (dict,list)):
                if isinstance(value, list) and isinstance(value[0], dict):

                    value = value[0]

                self.JSON_treating(value)

            data.append(value)
        except:
            data.append(value)
            Dict = OrderedDict(zip(headers,dados))

            self.JSON_treating(Dict)


    return Dict
这里的目标是,每次代码找到嵌套表时,都会获取其标题和值,并将标题附加到数据的标题和值中,然后在完成后,代码将按顺序连接标题和值

我的问题是,我做不好,看。。。要么代码没有得到所有嵌套的,要么代码根本不工作。 请帮忙 谢谢

编辑: 下面是一个输入JSON的示例

[
{
    "status": 0, 
    "payment_value": 0, 
    "date": "2018-07-28 12:18:00", 
    "Payment": [
        {
            "payment_general": 0, 
            "code": 1, 
            "total_value": 0, 
            "payment_form": "card"
        }
    ], 
    "id__": "", 
    "Product": [
        {
            "sku": "00000", 
            "ID_delivery": null, 
            "ammount": 1, 
            "unitary_value": 55.34, 
            "discount_name": null, 
            "delivery_form": null, 
            "discount_ammount": 0, 
            "delivery_charge": 20.34, 
            "taxes": 0, 
            "comission": null, 
            "id__discount": null
        }
    ], 
    "client": {
        "delivery": {
            "phone": [
                "1", 
                "2"
            ], 
            "fax": null, 
            "name": "A name here", 
            "state_tax": "free", 
            "address": {
                "reference": "a reference here", 
                "complement": "a complement here", 
                "block": "N123", 
                "city": "New York", 
                "road_name": "a road name", 
                "number": "413", 
                "postal_code": "123234", 
                "country": "US", 
                "State": "NY"
            }, 
            "email": "", 
            "document": ""
        }, 
        "taxation": {
            "phones": [
                "1", 
                "2"
            ], 
            "fax": null, 
            "type": "AN", 
            "nome": "a name here", 
            "state_demand": "A-B", 
            "birth_date": "1996-04-01", 
            "sex": "F-M", 
            "address": {
                "reference": "a reference here", 
                "complement": "a complement here", 
                "block": "N123", 
                "city": "New York", 
                "road_name": "a road name", 
                "number": "413", 
                "postal_code": "123234", 
                "country": "US", 
                "State": "NY"
            }, 
            "email": "a e mail ", 
            "document": "a document"
        }
    }, 
    "delivery_prevision": 10
}]
以下是我所需要的:

{
"status": 0, 
"payment_value": 0, 
"date": "2018-07-28 12:18:00", 
"Payment": ,

"payment_general": 0, 
"code": 1, 
"total_value": 0, 
"payment_form": "card", 
"id__": "", 
"Product":, 
"NDE": "00000", 
"ID_delivery": null, 
"ammount": 1, 
"unitary_value": 55.34, 
"discount_name": null, 
"delivery_form": null, 
"discount_ammount": 0, 
"delivery_charge": 20.34, 
"taxes": 0, 
"comission": null, 
"id__discount": null,
"client": ,
"delivery": ,
"phone": "1" "2",
"fax": null, 
"name": "A name here", 
"state_tax": "free", 
"address": ,
"reference": "a reference here", 
"complement": "a complement here", 
"block": "N123", 
"city": "New York", 
"road_name": "a road name", 
"number": "413", 
"postal_code": "123234", 
"country": "US", 
"State": "NY",

"taxation": ,
"phones": "1",  "2"

"fax": null, 
"type": "AN", 
"nome": "a name here", 
"state_demand": "A-B", 
"birth_date": "1996-04-01", 
"sex": "F-M", 
"address": ,
"reference": 
"a reference here", 
"complement": "a complement here", 
"block": "N123", 
"city": "New York", 
"road_name": "a road name", 
"number": "413", 
"postal_code": "123234", 
"country": "US", 
"State": "NY",
"email": "a e mail ", 
"document": "a document",
"delivery_prevision": 10
}
这是给你的吗

class UnknownClassYouNeverShowed(object):
    def JSON_Treating(self, json_data):
        flat_dict = {}
        if isinstance(json_data, list):
            for item in json_data:
                if isinstance(item, (list, dict)):
                    flat_dict.update(self.JSON_Treating(item))
            return flat_dict

        for key, value in json_data.iteritems():
            if isinstance(value, (list, dict)):
                flat_dict.update(self.JSON_Treating(value))
            else:
                flat_dict[key] = value
        return flat_dict
如果您真的只有json字符串,请加载它并使用:
importJSON;data=json.load(一些json字符串)
。。你明白了


我得到这个:

import pprint
unknown = UnknownClassYouNeverShowed()
pprint.pprint(unknown.JSON_Treating(data))
输出:

{'ID_delivery': None,
 'State': 'NY',
 'ammount': 1,
 'birth_date': '1996-04-01',
 'block': 'N123',
 'city': 'New York',
 'code': 1,
 'comission': None,
 'complement': 'a complement here',
 'country': 'US',
 'date': '2018-07-28 12:18:00',
 'delivery_charge': 20.34,
 'delivery_form': None,
 'delivery_prevision': 10,
 'discount_ammount': 0,
 'discount_name': None,
 'document': 'a document',
 'email': 'a e mail ',
 'fax': None,
 'id__': '',
 'id__discount': None,
 'name': 'A name here',
 'nome': 'a name here',
 'number': '413',
 'payment_form': 'card',
 'payment_general': 0,
 'payment_value': 0,
 'postal_code': '123234',
 'reference': 'a reference here',
 'road_name': 'a road name',
 'sex': 'F-M',
 'sku': '00000',
 'state_demand': 'A-B',
 'state_tax': 'free',
 'status': 0,
 'taxes': 0,
 'total_value': 0,
 'type': 'AN',
 'unitary_value': 55.34}

您是否正在尝试展平json?显示一个输入示例如何?--还有可能是期望的输出?还有,py2?是的,我正在尝试将json扁平化为一个dict。