简体   繁体   English

给定密钥的名称,如何从geojson object 中获取值

[英]how to get value from geojson object given the name of the key

the below posted geojson is a result of a call to the back-end and i received the data in the data object and by accessing下面发布的 geojson 是调用后端的结果,我收到了数据 object 中的数据并通过访问

data["geojson"][0]

i got the result posted below.我得到了下面发布的结果。 my question is how to get the values of the following keys in the below posted geojson我的问题是如何在下面发布的 geojson 中获取以下键的值

type
features

i tried the following:我尝试了以下方法:

data["geojson"][0].type
data["geojson"][0]["type"]
data["geojson"][0].features
data["geojson"][0]["features"]

and all of them returned undefined.并且所有这些都返回未定义。 please let me know how to get the valued contained in the type and features keys请让我知道如何获取typefeatures键中包含的值

geojson :地理JSON

  {
  "type": "FeatureCollection",
  "features": [
{
  "type": "Feature",
  "properties": {
    "treeID": "35",
    "XTOP": "289361.36",
    "YTOP": "5627942.71",
    "ZTOP": "8.96",
    "Zmax": "8.96",
    "Zmean": "3.88565217391304",
    "Zmin": "-0.22",
    "ID": "1"
  },
  "geometry": {
    "type": "Polygon",
    "coordinates": [ [ [6.01294596725522,50.7649619453627], [6.01294655084025,50.7649616904627], [6.01294480728641,50.7649601165684], [6.01290750549077,50.7649556522326], [6.01290401670994,50.7649636585763], [6.01290754960196,50.7649660879154], [6.01292208328504,50.7649672700401], [6.01292595809794,50.764966559787], [6.01294596725522,50.7649619453627] ] ]
  }
},
{
  "type": "Feature",
  "properties": {
    "treeID": "36",
    "XTOP": "289364.9",
    "YTOP": "5627941.39",
    "ZTOP": "8.61",
    "Zmax": "8.61",
    "Zmean": "1.70708487084871",
    "Zmin": "-0.23",
    "ID": "2"
  },
  "geometry": {
    "type": "Polygon",
    "coordinates": [ [ [6.01303976949995,50.7649411418857], [6.01305108930002,50.7649371142921], [6.01305889189702,50.764932636732], [6.01301768721015,50.7649026156833], [6.01295732492073,50.7649261653276], [6.01294076115474,50.7649457103209], [6.01294266753767,50.7649580827532], [6.01295011530617,50.764959173193], [6.01295547908076,50.764959580542], [6.01298047870417,50.7649545543143], [6.01303976949995,50.7649411418857] ] ]
  }
},
{
  "type": "Feature",
  "properties": {
    "treeID": "44",
    "XTOP": "289371.82",
    "YTOP": "5627938.69",
    "ZTOP": "6.68",
    "Zmax": "6.68",
    "Zmean": "0.923793103448276",
    "Zmin": "-0.2",
    "ID": "3"
  },
  "geometry": {
    "type": "Polygon",
    "coordinates": [ [ [6.01309078592578,50.7649173526471], [6.01307507511104,50.7649146111696], [6.01306638577937,50.7649151980216], [6.01305830553487,50.7649284838627], [6.01307735761254,50.7649322105161], [6.01309522551646,50.7649278110408], [6.01309078592578,50.7649173526471] ] ]
  }
},
{
  "type": "Feature",
  "properties": {
    "treeID": "24",
    "XTOP": "289387.04",
    "YTOP": "5627931.21",
    "ZTOP": "11.13",
    "Zmax": "11.13",
    "Zmean": "2.8069921875",
    "Zmin": "-0.4",
    "ID": "4"
  },
  "geometry": {
    "type": "Polygon",
    "coordinates": [ [ [6.01328734454998,50.7648331572413], [6.01328584413784,50.7648322192551], [6.01328139148204,50.7648330946158], [6.01321534224203,50.7648657637964], [6.01320827030382,50.7648877109824], [6.01321376130126,50.764892799138], [6.0132465933806,50.7648895028071], [6.01332086831076,50.7648724264007], [6.01332608767231,50.764868422333], [6.01332753378007,50.7648657608056], [6.01328734454998,50.7648331572413] ] ]
  }
},
{
  "type": "Feature",
  "properties": {
    "treeID": "26",
    "XTOP": "289385.57",
    "YTOP": "5627933.28",
    "ZTOP": "9.45",
    "Zmax": "9.45",
    "Zmean": "2.38978260869565",
    "Zmin": "-0.17",
    "ID": "5"
  },
  "geometry": {
    "type": "Polygon",
    "coordinates": [ [ [6.01327356674159,50.7648891147094], [6.01328421708838,50.7648866890897], [6.01330010250613,50.7648822387717], [6.01327865741243,50.7648826786135], [6.01325645133365,50.7648861573501], [6.01323567273339,50.7648894927636], [6.01321953843652,50.7648933969757], [6.01321456939895,50.7648956983472], [6.01322071837631,50.7648993641185], [6.01323256304289,50.7648982284572], [6.01327356674159,50.7648891147094] ] ]
  }
},
{
  "type": "Feature",
  "properties": {
    "treeID": "27",
    "XTOP": "289385.38",
    "YTOP": "5627929.41",
    "ZTOP": "8.65",
    "Zmax": "8.65",
    "Zmean": "1.83042253521127",
    "Zmin": "-0.16",
    "ID": "6"
  },
  "geometry": {
    "type": "Polygon",
    "coordinates": [ [ [6.01328506051132,50.7648178066857], [6.01328365738379,50.7648153419914], [6.0132786226098,50.7648142234655], [6.01327293558325,50.7648144375174], [6.01326339665165,50.7648150026084], [6.01325726764683,50.7648154751905], [6.01324869545512,50.7648275790463], [6.01325507746368,50.7648387168901], [6.01327269485743,50.7648449254249], [6.01328770190544,50.7648498077109], [6.01328895916927,50.7648501097939], [6.01329324733273,50.7648495900291], [6.01329357632041,50.7648488788368], [6.01329338905776,50.7648406883115], [6.01328506051132,50.7648178066857] ] ]
  }
},
{
  "type": "Feature",
  "properties": {
    "treeID": "22",
    "XTOP": "289387.77",
    "YTOP": "5627931.51",
    "ZTOP": "11.13",
    "Zmax": "11.13",
    "Zmean": "5.20431818181818",
    "Zmin": "-0.19",
    "ID": "7"
  },
  "geometry": {
    "type": "Polygon",
    "coordinates": [ [ [6.01333437641733,50.7648741219068], [6.01333561090581,5

I think this will help you.我想这会对你有所帮助。

import json  
with open('geojson.json') as f:
    data = json.load(f)
for feature in data['features']:
    print (feature['geometry']['type'])
print (data['type'])

geojson.json geojson.json

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {
        "treeID": "35",
        "XTOP": "289361.36",
        "YTOP": "5627942.71",
        "ZTOP": "8.96",
        "Zmax": "8.96",
        "Zmean": "3.88565217391304",
        "Zmin": "-0.22",
        "ID": "1"
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              6.01294596725522,
              50.7649619453627
            ],
            [
              6.01294655084025,
              50.7649616904627
            ],
            [
              6.01294480728641,
              50.7649601165684
            ],
            [
              6.01290750549077,
              50.7649556522326
            ],
            [
              6.01290401670994,
              50.7649636585763
            ],
            [
              6.01290754960196,
              50.7649660879154
            ],
            [
              6.01292208328504,
              50.7649672700401
            ],
            [
              6.01292595809794,
              50.764966559787
            ],
            [
              6.01294596725522,
              50.7649619453627
            ]
          ]
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "treeID": "36",
        "XTOP": "289364.9",
        "YTOP": "5627941.39",
        "ZTOP": "8.61",
        "Zmax": "8.61",
        "Zmean": "1.70708487084871",
        "Zmin": "-0.23",
        "ID": "2"
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              6.01303976949995,
              50.7649411418857
            ],
            [
              6.01305108930002,
              50.7649371142921
            ],
            [
              6.01305889189702,
              50.764932636732
            ],
            [
              6.01301768721015,
              50.7649026156833
            ],
            [
              6.01295732492073,
              50.7649261653276
            ],
            [
              6.01294076115474,
              50.7649457103209
            ],
            [
              6.01294266753767,
              50.7649580827532
            ],
            [
              6.01295011530617,
              50.764959173193
            ],
            [
              6.01295547908076,
              50.764959580542
            ],
            [
              6.01298047870417,
              50.7649545543143
            ],
            [
              6.01303976949995,
              50.7649411418857
            ]
          ]
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "treeID": "44",
        "XTOP": "289371.82",
        "YTOP": "5627938.69",
        "ZTOP": "6.68",
        "Zmax": "6.68",
        "Zmean": "0.923793103448276",
        "Zmin": "-0.2",
        "ID": "3"
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              6.01309078592578,
              50.7649173526471
            ],
            [
              6.01307507511104,
              50.7649146111696
            ],
            [
              6.01306638577937,
              50.7649151980216
            ],
            [
              6.01305830553487,
              50.7649284838627
            ],
            [
              6.01307735761254,
              50.7649322105161
            ],
            [
              6.01309522551646,
              50.7649278110408
            ],
            [
              6.01309078592578,
              50.7649173526471
            ]
          ]
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "treeID": "24",
        "XTOP": "289387.04",
        "YTOP": "5627931.21",
        "ZTOP": "11.13",
        "Zmax": "11.13",
        "Zmean": "2.8069921875",
        "Zmin": "-0.4",
        "ID": "4"
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              6.01328734454998,
              50.7648331572413
            ],
            [
              6.01328584413784,
              50.7648322192551
            ],
            [
              6.01328139148204,
              50.7648330946158
            ],
            [
              6.01321534224203,
              50.7648657637964
            ],
            [
              6.01320827030382,
              50.7648877109824
            ],
            [
              6.01321376130126,
              50.764892799138
            ],
            [
              6.0132465933806,
              50.7648895028071
            ],
            [
              6.01332086831076,
              50.7648724264007
            ],
            [
              6.01332608767231,
              50.764868422333
            ],
            [
              6.01332753378007,
              50.7648657608056
            ],
            [
              6.01328734454998,
              50.7648331572413
            ]
          ]
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "treeID": "26",
        "XTOP": "289385.57",
        "YTOP": "5627933.28",
        "ZTOP": "9.45",
        "Zmax": "9.45",
        "Zmean": "2.38978260869565",
        "Zmin": "-0.17",
        "ID": "5"
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              6.01327356674159,
              50.7648891147094
            ],
            [
              6.01328421708838,
              50.7648866890897
            ],
            [
              6.01330010250613,
              50.7648822387717
            ],
            [
              6.01327865741243,
              50.7648826786135
            ],
            [
              6.01325645133365,
              50.7648861573501
            ],
            [
              6.01323567273339,
              50.7648894927636
            ],
            [
              6.01321953843652,
              50.7648933969757
            ],
            [
              6.01321456939895,
              50.7648956983472
            ],
            [
              6.01322071837631,
              50.7648993641185
            ],
            [
              6.01323256304289,
              50.7648982284572
            ],
            [
              6.01327356674159,
              50.7648891147094
            ]
          ]
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "treeID": "27",
        "XTOP": "289385.38",
        "YTOP": "5627929.41",
        "ZTOP": "8.65",
        "Zmax": "8.65",
        "Zmean": "1.83042253521127",
        "Zmin": "-0.16",
        "ID": "6"
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              6.01328506051132,
              50.7648178066857
            ],
            [
              6.01328365738379,
              50.7648153419914
            ],
            [
              6.0132786226098,
              50.7648142234655
            ],
            [
              6.01327293558325,
              50.7648144375174
            ],
            [
              6.01326339665165,
              50.7648150026084
            ],
            [
              6.01325726764683,
              50.7648154751905
            ],
            [
              6.01324869545512,
              50.7648275790463
            ],
            [
              6.01325507746368,
              50.7648387168901
            ],
            [
              6.01327269485743,
              50.7648449254249
            ],
            [
              6.01328770190544,
              50.7648498077109
            ],
            [
              6.01328895916927,
              50.7648501097939
            ],
            [
              6.01329324733273,
              50.7648495900291
            ],
            [
              6.01329357632041,
              50.7648488788368
            ],
            [
              6.01329338905776,
              50.7648406883115
            ],
            [
              6.01328506051132,
              50.7648178066857
            ]
          ]
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "treeID": "22",
        "XTOP": "289387.77",
        "YTOP": "5627931.51",
        "ZTOP": "11.13",
        "Zmax": "11.13",
        "Zmean": "5.20431818181818",
        "Zmin": "-0.19",
        "ID": "7"
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              6.01333437641733,
              50.7648741219068
            ],
            [
              6.01333561090581,
              5
            ]
          ]
        ]
      }
    }
  ]
}


In object you can access the value by key, And the key is string在 object 中,您可以通过键访问值,键是字符串

now, in your data object, there is no key like "geojson", there are only "type" and "featrues"现在,在您的数据 object 中,没有像“geojson”这样的键,只有“类型”和“特征”

so if I do write, I will data.features.[index].type.所以如果我写,我会 data.features.[index].type。 and This is same with data["featrues"][index]["type"]这与 data["featrues"][index]["type"] 相同

In this case, index is some number like 0, 1, 2..在这种情况下,索引是一些数字,如 0、1、2..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM