有没有一种方法可以轻松地在python中处理json树数据

时间:2019-11-27 10:07:42

标签: python json for-loop web-scraping

有没有一种方法可以在python中处理json数据树。 可以说我的json树如下:

    "owner": {
          "location": {
                 "longitude":"546561.65101",
                 "city":{
                      "zipcode":"546879",
                      "city":"Boston"
    }}},

我的python脚本看起来像这样:

import json

with urlopen("example.com/example.json") as response:
    source = response.read()

data=json.loads(source)

#lets say i want to extract specific json data with a for loop

for items in data:
    city=items['HOW CAN I ACCESS THE CITY TAG WITHIN THE PARENT TAGS']

1 个答案:

答案 0 :(得分:0)

x={
"owner": {
  "location": {
         "longitude":"546561.65101",
         "city":{
              "zipcode":"546879",
              "city":"Boston"
}}}}

您可以使用

x['owner']['location']['city']['city']

这将输出为“波士顿”