简体   繁体   中英

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

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 :

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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