如何向JSON Python Django添加变量
我想将ID添加到从Google Maps API检索到的骑乘中 通过上面的代码,我得到了这个,这就是我想要的。 如果我连接ID,我会得到这个 这是带有串联的代码如何向JSON Python Django添加变量,python,json,django,django-rest-framework,Python,Json,Django,Django Rest Framework,我想将ID添加到从Google Maps API检索到的骑乘中 通过上面的代码,我得到了这个,这就是我想要的。 如果我连接ID,我会得到这个 这是带有串联的代码 class GetRouteDriver(APIView): authentication_classes = (TokenAuthentication,) permission_classes = (IsAuthenticated,) def get(self, request, username
class GetRouteDriver(APIView):
authentication_classes = (TokenAuthentication,)
permission_classes = (IsAuthenticated,)
def get(self, request, username, origin, destination):
origin_lat, origin_lng = origin.split(',')
destination_lat, destination_lng = destination.split(',')
url = 'https://maps.googleapis.com/maps/api/directions/json?origin=' + origin + '&destination=' + destination + '&key'+ GOOGLE_KEY
request_data = urllib2.urlopen(url)
serialized_data = request_data.read()
id = '1'
ride = json.loads(serialized_data)
return Response( ride, status=status.HTTP_200_OK )
我刚刚做了这个,我从google响应中加载json,然后像下面的代码中那样进行jason.dump
class GetRouteDriver(APIView):
authentication_classes = (TokenAuthentication,)
permission_classes = (IsAuthenticated,)
def get(self, request, username, origin, destination):
origin_lat, origin_lng = origin.split(',')
destination_lat, destination_lng = destination.split(',')
url = 'https://maps.googleapis.com/maps/api/directions/json?origin=' + origin + '&destination=' + destination + '&key'+ GOOGLE_KEY
request_data = urllib2.urlopen(url)
#serialized_data = request_data.read()
serialized_data = json.dumps({'ride_id': 1, 'route': request_data.read()})
ride = json.loads(serialized_data)
return Response( ride, status=status.HTTP_200_OK )
现在我得到了我想要的
到目前为止,您尝试了什么?什么不起作用?你有错误吗?帮助我们,帮助你。
class GetRouteDriver(APIView):
authentication_classes = (TokenAuthentication,)
permission_classes = (IsAuthenticated,)
def get(self, request, username, origin, destination):
origin_lat, origin_lng = origin.split(',')
destination_lat, destination_lng = destination.split(',')
url = 'https://maps.googleapis.com/maps/api/directions/json?origin=' + origin + '&destination=' + destination + '&key'+ GOOGLE_KEY
request_data = urllib2.urlopen(url)
#serialized_data = request_data.read()
serialized_data = json.dumps( { 'ride_id': 1 , 'route': json.loads(request_data.read()) })
route = json.loads(serialized_data)
return Response( route, status=status.HTTP_200_OK )