给定键名,如何从geojson对象中获取值

时间:2021-04-23 08:34:31

标签: javascript json angular geojson

下面发布的 geojson 是调用后端的结果,我收到了数据对象中的数据并通过访问

data["geojson"][0]

我得到了下面发布的结果。 我的问题是如何在下面发布的 geojson 中获取以下键的值

type
features

我尝试了以下方法:

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

并且所有这些都返回未定义。 请让我知道如何获取包含在 typefeatures

中的值

geojson

  {
  "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

2 个答案:

答案 0 :(得分:0)

我认为这会对您有所帮助。

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

{
  "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
            ]
          ]
        ]
      }
    }
  ]
}


答案 1 :(得分:-2)

在对象中,您可以通过键访问值,并且键是字符串

现在,在您的数据对象中,没有像“geojson”这样的键,只有“类型”和“特征”

所以如果我写,我会data.features.[index].type。这与 data["featrues"][index]["type"]

相同

在这种情况下,索引是一些数字,如 0、1、2..