Bagaimana untuk mengisi alamat auto dalam json

PHPz
Lepaskan: 2024-02-05 22:30:07
ke hadapan
1230 orang telah melayarinya

Bagaimana untuk mengisi alamat auto dalam json

Kandungan soalan

Ini objek json saya

[
  {
    "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"
      }
    ]
  }
]
Salin selepas log masuk

Saya ingin mencipta json berikut untuk menambahkan medan alamat ke json secara dinamik.

[
  {
    "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.
Salin selepas log masuk


Jawapan betul


Saya cuba sedaya upaya untuk menyelesaikannya. Lihat jika ini membantu. Tulis kod dalam json pendek. Tetapi anda boleh menggunakan json bersarang penuh sebagai input.

# 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)
Salin selepas log masuk

Atas ialah kandungan terperinci Bagaimana untuk mengisi alamat auto dalam json. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

sumber:stackoverflow.com
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan