【问题标题】:Is there a way to easily handle json tree data in python有没有办法在python中轻松处理json树数据
【发布时间】:2019-11-27 10:07:42
【问题描述】:

有没有办法在 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']

【问题讨论】:

  • 因为这只是 json/字典数据,所以你可以使用 items.owner.location.city OR items['owner']['location']['city']

标签: python json for-loop web-scraping


【解决方案1】:
x={
"owner": {
  "location": {
         "longitude":"546561.65101",
         "city":{
              "zipcode":"546879",
              "city":"Boston"
}}}}

你可以使用,

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

将输出为“波士顿”

【讨论】:

    猜你喜欢
    • 2017-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多