美文网首页
python: json转模型: dict 合并(取合集)

python: json转模型: dict 合并(取合集)

作者: LiYaoPeng | 来源:发表于2022-12-16 22:03 被阅读0次

python学习中,多多指教

model2 相对于 model1 ,多出了recommend等字段,在json转模型中,需要对字典进行合并,才能保证转出的模型不丢字段

[
    {
        "product":"衣服",
        "type":{
            "type":"白色上衣",
            "activity":[
                "秒杀活动",
                "加购送袜子"
            ],
            "company":{
                "name":"靓仔上衣",
                "awards":[
                    "最具人气奖",
                    "销量第一"
                ]
            },
            "details":{
                "size":"xxl",
                "color":"白色"
            }
        }
    },
    {
        "product":"鞋子",
        "type":{
            "type":"黑色鞋子",
            "activity":[
                "运动专属活动"
            ],
            "company":{
                "name":"耐磨谢业"
            },
            "recommend":{
                "title":"更多为你推荐",
                "link":"www.baidu.com"
            }
        }
    }
]

合并后效果:

[
    {
        "product":"衣服",
        "type":{
            "type":"黑色鞋子",
            "activity":[
                "秒杀活动",
                "加购送袜子",
                "运动专属活动"
            ],
            "company":{
                "name":"靓仔上衣",
                "awards":[
                    "最具人气奖",
                    "销量第一"
                ]
            },
            "recommend":{
                "title":"更多为你推荐",
                "link":"www.baidu.com"
            },
            "details":{
                "size":"xxl",
                "color":"白色"
            }
        }
    }
]

合并代码:


def isBaseTypeWithValue(value):
    return isinstance(value, bool) | isinstance(value, int) | isinstance(value, float) | isinstance(value, str)


def flattenMap(map) -> dict:
    result: dict = {}
    if isinstance(map, list):
        result = mergetList(map)

    if isinstance(map, dict):
        flatten = {}
        for key, value in map.items():
            if (isBaseTypeWithValue(value)) or flatten.get(key) == None:
                flatten[key] = map[key]
                continue
            else:
                merge_map = flattenMap(map)
                flatten = mergeMap(flatten, merge_map)

        result = flatten
    return result


def mergetList(list: list) -> list:
    if len(list) <= 1:
        return list
    result = {}
    for value in list:
        if isBaseTypeWithValue(value):
            continue
        result = mergeMap(result, value)
    if len(result) == 0:
        return list
    return [result]


def mergeMap(map1: dict, map2: dict):
    if map1 is None:
        return map2
    if map2 is None:
        return map1
    if isinstance(map1, list) and isinstance(map2, list):
        l1 = mergetList(map1)
        l2 = mergetList(map2)
        return mergetList(l1 + l2)

    if isinstance(map1, list) and isinstance(map2, dict):
        l: list = map1
        list.append(map2)
        return mergetList(l)

    if isinstance(map1, list) and isBaseTypeWithValue(map2):
        l: list = map1
        list.append(map2)
        return mergetList(l)

    if isinstance(map1, dict) and isinstance(map1, list):
        l: list = map2
        l.append(map1)
        return mergetList(l)

    if isinstance(map1, dict) and isBaseTypeWithValue(map2):
        return map1

    if isBaseTypeWithValue(map1) and isinstance(map2, list):
        l: list = map2
        l.append(map1)
        return mergetList(l)

    if isBaseTypeWithValue(map1) and isBaseTypeWithValue(map1):
        return map1

    result = map1.copy()
    for key, value in map2.items():
        map1_value = map1.get(key)
        if map1_value is not None:
            # 相同key
            if (isBaseTypeWithValue(value)) == False and isBaseTypeWithValue(map1_value):
                result[key] = flattenMap(value)
            elif (isBaseTypeWithValue(value)) and isBaseTypeWithValue(map1_value):
                continue
            else:
                result[key] = mergeMap(value, map1_value)
        else:
            result[key] = value
    result = flattenMap(result)
    return result


相关文章

网友评论

      本文标题:python: json转模型: dict 合并(取合集)

      本文链接:https://www.haomeiwen.com/subject/bbirqdtx.html