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

Javascript 如何访问给定键的geojson响应或从中获取值,javascript,python,json,angular,geojson,Javascript,Python,Json,Angular,Geojson,在下面的geojson响应中,我希望能够访问以下内容 type and segments return data["type"] //does not work i receive an error return data["features"][0]["properties"]["segments"] //does not work i receive an error 为了做到这一点,我做了以

在下面的geojson响应中,我希望能够访问以下内容

type and segments
    return data["type"] //does not work i receive an error
    return data["features"][0]["properties"]["segments"] //does not work i receive an error
为了做到这一点,我做了以下几点

type and segments
    return data["type"] //does not work i receive an error
    return data["features"][0]["properties"]["segments"] //does not work i receive an error
请告诉我如何在给定密钥的情况下访问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"}}}
@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
代码

{"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"}}}
@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
@app.route(“/getRouteFromOriginToDestination/”,方法=['GET'])
def getRouteFromOriginToDestination(原始和目标PSCoords):
#api\u key=request.args.get('api\u key')
#starttat,starting=request.args.get('start').split(','))
#end1Lat,endLng=request.args.get('end').split(','))
全局坐标
gpsCoordinates=原始和目标PSCoords
isCohesiveData=CheckDataNe聚力(gpsCoordinates)
如果(isCohesiveData):
logging.info(“数据是内聚的”)
data=fetchDataForURL(getBaseURL(gpsCoordinates))
其他:
打印(“数据不连贯”)
中止(400,“未找到记录”)
#距离=浮动(距离)
#距离=距离/1000
返回数据

您需要首先解析json:

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

execuse me我对dataAsJSON[“features”][0][“geometry”][“coordinates”]尝试了相同的方法,但它返回了一个错误。请您帮助我展示一下dataAsJSON[“features”]的效果。您知道,Python的一个优点是能够在命令行上处理数据。然后,每个操作的结果都可以在特殊变量
中找到。“returns a error”(返回错误)没有帮助。请下次告诉我们什么错误。