【问题标题】:How to remove \n from API response or get the response in appropriate JSON format when GET method is requested via APIView class当通过 APIView 类请求 GET 方法时,如何从 API 响应中删除 \n 或以适当的 JSON 格式获取响应
【发布时间】:2020-11-07 13:28:31
【问题描述】:

当我在邮递员中调用 Google 的 Distance Matrix API 时,我得到了正确的 JSON 响应。

API 端点

https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=Washington,DC&destinations=New+York+City,NY&key=[API KEY]

回应

{
   "destination_addresses": [
      "New York, NY, USA"
   ],
   "origin_addresses": [
      "Washington, DC, USA"
   ],
   "rows": [
      {
         "elements": [
            {
               "distance": {
                  "text": "228 mi",
                  "value": 367435
               },
               "duration": {
                  "text": "3 hours 43 mins",
                  "value": 13385
               },
               "status": "OK"
            }
         ]
      }
   ],

但是当我在我的 DRF 中使用 APIView 类做同样的事情时,我得到了一些奇怪的\n 作为响应。

APIView

import requests
from rest_framework.views import APIView
from rest_framework.response import Response


class TestView(APIView):
    
    def get(self, request, format=None):
        data={}
        data['destinations'] =[
            'Nashik+Maharashtra',
            'Mumbai+Maharashtra',
            'Nagpur+Maharashtra',
            'Malegaon+Maharashtra'
        ]
        data['origins'] = "Pune+Maharashtra"
        params ={
            "units":"metrix",
            "origins":data['origins'],
            "destinations":data['destinations'],
            "key":"AIzaSyDqf9R6mFSDrwqjM7iXv289o9KEdvEEmM8"
            }       

        response = requests.get("https://maps.googleapis.com/maps/api/distancematrix/json?", params)
        return Response(response.text)

回应


"{\n   \"destination_addresses\" : [ \"Nashik, Maharashtra, India\" ],\n   \"origin_addresses\" : [ \"Pune, Maharashtra, India\" ],\n   \"rows\" : [\n      {\n         \"elements\" : [\n            {\n               \"distance\" : {\n                  \"text\" : \"212 km\",\n                  \"value\" : 212479\n               },\n               \"duration\" : {\n                  \"text\" : \"4 hours 19 mins\",\n                  \"value\" : 15531\n               },\n               \"status\" : \"OK\"\n            }\n         ]\n      }\n   ],\n   \"status\" : \"OK\"\n}\n"

如何获得适当的 JSON 响应。

【问题讨论】:

    标签: python django-rest-framework


    【解决方案1】:

    “奇怪的\n”是换行符的转义序列。由于响应是一个字符串,它使用\n 来表示一个换行符。为了更好地理解我的意思,请尝试打印响应,它会以与您的第一个响应相同的格式显示给您。

    【讨论】:

      【解决方案2】:

      使用response.json() 代替response.text

      【讨论】:

        【解决方案3】:
        res.json(JSON.parse(response));
        

        并且,在 chrome 扩展中使用 https://chrome.google.com/webstore/detail/json-formatter/bcjindcccaagfpapjjmafapmmgkkhgoa?hl=en-US

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-07-17
          • 2020-09-05
          • 2017-08-22
          • 2014-09-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多