简体   繁体   English

给定键如何从geojson响应中访问或获取值

[英]how to access or get value from geojson response given a key

in the below geojson response, i want to have access on the following在下面的 geojson 响应中,我想访问以下内容

type and segments

to achive that i did the following为了实现我做了以下

    return data["type"] //does not work i receive an error
    return data["features"][0]["properties"]["segments"] //does not work i receive an error

please let me know how to access or get value from geojson response given a key geojson :请让我知道如何在给定关键geojson的情况下访问或从 geojson 响应中获取值:

{"type":"FeatureCollection","features":[{"bbox":[29.924118,31.205141,29.924728,31.20539],"type":"Feature","properties":{"segments":[{"distance":64.4,"duration":3.9,"steps":[{"distance":64.4,"duration":3.9,"type":11,"instruction":"Head southwest on شارع جمال عبد الناصر","name":"شارع جمال عبد الناصر","way_points":[0,3]},{"distance":0.0,"duration":0.0,"type":10,"instruction":"Arrive at شارع جمال عبد الناصر, straight ahead","name":"-","way_points":[3,3]}]}],"summary":{"distance":64.4,"duration":3.9},"way_points":[0,3]},"geometry":{"coordinates":[[29.924728,31.20539],[29.924568,31.205325],[29.924493,31.205294],[29.924118,31.205141]],"type":"LineString"}}],"bbox":[29.924118,31.205141,29.924728,31.20539],"metadata":{"attribution":"openrouteservice.org | OpenStreetMap contributors","service":"routing","timestamp":1618549441274,"query":{"coordinates":[[29.924728,31.205391],[29.924118,31.205141]],"profile":"driving-car","format":"json"},"engine":{"version":"6.4.1","build_date":"2021-04-12T07:11:51Z","graph_date":"1970-01-01T00:00:00Z"}}}

code :代码

@app.route("/getRouteFromOriginToDestination/<string:originAndDestinationGPSCoords>", methods=['GET'] )
def getRouteFromOriginToDestination(originAndDestinationGPSCoords):
#api_key = request.args.get('api_key')
# startLat, startLng= request.args.get('start').split(',')
# end1Lat, endLng = request.args.get('end').split(',')
global gpsCoordinates
gpsCoordinates = originAndDestinationGPSCoords
isCohesiveData = checkDataCohesion(gpsCoordinates)
if (isCohesiveData):
    logging.info("data is cohesive")
    data = fetchDataForURL(getBaseURL(gpsCoordinates))
else:
    print("data is not cohesive")
    abort(400, 'Record not found')   

#dist = float(distance)
#dist = dist/1000
return data

You need to parse the json first:您需要先解析 json:

>>> import json
>>> data = json.loads('{"type":"FeatureCollection"}')
>>> data['type']
'FeatureCollection'

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

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