如何自动填充json中的地址

PHPz
发布: 2024-02-05 22:30:07
转载
1230 人浏览过

如何自动填充json中的地址

问题内容

这是我的 json 对象

[
  {
    "person": "abc",
    "city": "united states",
    "facebooklink": "link",
    "united states": [
      {
        "person": "cdf",
        "city": "ohio",
        "facebooklink": "link",
        "ohio": [
          {
            "person": "efg",
            "city": "clevland",
            "facebooklink": "link",
            "clevland": [
              {
                "person": "jkl",
                "city": "street a",
                "facebooklink": "link",
                "street a": [
                  {
                    "person": "jkl",
                    "city": "house 1",
                    "facebooklink": "link"
                  }
                ]
              }
            ]
          },
          {
            "person": "ghi",
            "city": "columbus",
            "facebooklink": "link"
          }
        ]
      },
      {
        "person": "abc",
        "city": "washington",
        "facebooklink": "link"
      }
    ]
  }
]
登录后复制

我想创建以下 json,将地址字段动态添加到 json 中。

[
  {
    "person": "abc",
    "city": "united states",
    "facebooklink": "link",
    "address": "united states",
    "united states": [
      {
        "person": "cdf",
        "city": "ohio",
        "facebooklink": "link",
        "address": "united states/ohio",
        "ohio": [
          {
            "person": "efg",
            "city": "clevland",
            "facebooklink": "link",
            "address": "united states/ohio/clevland",
            "clevland": [
              {
                "person": "jkl",
                "city": "Street A",
                "facebooklink": "link",
                "address": "united states/ohio/clevland/Street A",
                "Street A": [
                  {
                    "person": "jkl",
                    "city": "House 1",
                    "facebooklink": "link",
                    "address": "united states/ohio/clevland/Street A/House 1"
                  }
                ]
              }
            ]
          },
          {
            "person": "ghi",
            "city": "columbus",
            "facebooklink": "link",
            "address": "united states/ohio/columbus"
          }
        ]
      },
      {
        "person": "abc",
        "city": "washington",
        "facebooklink": "link",
        "address": "united states/washington"
      }
    ]
  }
]
``

How can I achieve this in Python.
登录后复制


正确答案


我已经尽力解决了。看看这是否有帮助。 用短json编写代码。但您可以使用完整的嵌套 json 作为输入。

# import json

given_data = [
  {
    "person": "abc",
    "city": "united states",
    "facebooklink": "link",
    "united states": [
      {
        "person": "cdf",
        "city": "ohio",
        "facebooklink": "link"
      },
      {
        "person": "abc",
        "city": "washington",
        "facebooklink": "link"
      }
    ]
  }
]

PARENT_TOP = ""
city = "city"
address = 'address'

def update_address(parent_node, my_path=PARENT_TOP):
    if isinstance(parent_node, list):
        for child_node in parent_node:
            if isinstance(child_node, dict):
                city = child_node.get("city")
                new_path = f"{my_path}/{city}" if my_path else city
                child_node[address] = new_path
                grand_child = child_node.get(city)
                update_address(grand_child, new_path)
    elif isinstance(parent_node, dict):
        city = parent_node.get("city")
        new_path = f"{my_path}/{city}" if my_path else city
        parent_node[address] = new_path
        child = parent_node.get(city)
        update_address(child, new_path)
    else:
        pass

    return parent_node

if __name__ == '__main__':
    update_address(given_data)
    # output = json.dumps(json.loads(str(given_data)), indent=2)
    print(given_data)
登录后复制

以上是如何自动填充json中的地址的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:stackoverflow.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板