json - 我不明白服务器响应中 json 中的斜杠来自哪里

标签 json python-3.x rest http hug

在主函数中,我调用了一个函数,其中调用了另一个应用程序,结果我得到了 json 格式的数据。 但是我不明白每个双引号前面的斜线是从哪里来的"

在浏览器中,我看到带引号的数据 示例: {\"192.168.43.1\":[\"53\":{\"state\":\"open\"...

如果我不发送数据而是写入文件,则数据写入文件时不带斜杠 示例: {"192.168.43.1":["53":{"state":"open"...

这正常吗? 如何删除斜杠? 这个数据必须接受 另一个应用程序并反序列化它们。

def get_ip(ip, port):
   return os.system("some_app")

@hug.get('/scan')
def main(ip: hug.types.text, port: hug.types.text):
    json = get_ip(ip, port)

    #JUST FOR TEST WITH PARAM safe=False                                                
    return JsonResponse("{\"192.168.1.1\":[\"80\":{\"state\":\"open\",\"reason\":\"syn-ack\",\"name\":\"http\",\"product\":\"\"}]}", safe=False)

没有参数的错误 safe=False:

Traceback (most recent call last):
 File "/usr/lib/python3.6/wsgiref/handlers.py", line 137, in run
 self.result = application(self.environ, self.start_response)
 File "/usr/local/lib/python3.6/dist-packages/falcon/api.py", line 244, in __call__
responder(req, resp, **params)
File "/usr/local/lib/python3.6/dist-packages/hug/interface.py", line 793, in __call__
raise exception
File "/usr/local/lib/python3.6/dist-packages/hug/interface.py", line 766, in __call__
self.render_content(self.call_function(input_parameters), context, request, response, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/hug/interface.py", line 703, in call_function
return self.interface(**parameters)
 File "/usr/local/lib/python3.6/dist-packages/hug/interface.py", line 100, in __call__
return __hug_internal_self._function(*args, **kwargs)
File "script.py", line 181, in main
return JsonResponse("{\"192.168.1.1\":[\"80\":{\"state\":\"open\",\"reason\":\"syn-ack\",\"name\":\"http\",\"product\":\"\"}]}")
File "/usr/local/lib/python3.6/dist-packages/django/http/response.py", line 552, in __init__
'In order to allow non-dict objects to be serialized set the '
TypeError: In order to allow non-dict objects to be serialized set the safe parameter to False.

参数 safe=False 的错误:

return JsonResponse("{\"192.168.1.1\":[\"80\":{\"state\":\"open\",\"reason\":\"syn-ack\",\"name\":\"http\",\"product\":\"\"}]}", safe=False)


 File "/usr/lib/python3.6/wsgiref/handlers.py", line 137, in run
self.result = application(self.environ, self.start_response)
 File "/usr/local/lib/python3.6/dist-packages/falcon/api.py", line 244, in __call__
responder(req, resp, **params)
File "/usr/local/lib/python3.6/dist-packages/hug/interface.py", line 793, in __call__
raise exception
File "/usr/local/lib/python3.6/dist-packages/hug/interface.py", line 766, in __call__
self.render_content(self.call_function(input_parameters), context, request, response, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/hug/interface.py", line 703, in call_function
return self.interface(**parameters)
File "/usr/local/lib/python3.6/dist-packages/hug/interface.py", line 100, in __call__
return __hug_internal_self._function(*args, **kwargs)
File "script.py", line 181, in main
return JsonResponse("{\"192.168.1.1\":[\"80\":{\"state\":\"open\",\"reason\":\"syn-ack\",\"name\":\"http\",\"product\":\"\"}]}", safe=False)
File "/usr/local/lib/python3.6/dist-packages/django/http/response.py", line 559, in __init__
super().__init__(content=data, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/django/http/response.py", line 291, in __init__
self.content = content
File "/usr/local/lib/python3.6/dist-packages/django/http/response.py", line 321, in content
content = self.make_bytes(value)
File "/usr/local/lib/python3.6/dist-packages/django/http/response.py", line 236, in make_bytes
return bytes(value.encode(self.charset))
File "/usr/local/lib/python3.6/dist-packages/django/http/response.py", line 85, in charset
return settings.DEFAULT_CHARSET
File "/usr/local/lib/python3.6/dist-packages/django/conf/__init__.py", line 57, in __getattr__
self._setup(name)
File "/usr/local/lib/python3.6/dist-packages/django/conf/__init__.py", line 42, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting 
DEFAULT_CHARSET, but settings are not configured. You must either define the 
environment variable DJANGO_SETTINGS_MODULE or call settings.configure() 
before accessing settings.

最佳答案

您的 return "result {json}" 将返回包含 json 的字符串值。由于返回值是字符串,浏览器会显示额外的反斜杠以便正确处理双引号。

要解决此问题,您可以在客户端处理字符串响应并从字符串中提取 json 值。

JSON.parse(response);

但由于另一个应用程序需要 json 格式,你最好使用 JsonResponse 来确保返回的响应不是字符串而是 json 格式

from django.http import JsonResponse
return JsonResponse(json)

关于json - 我不明白服务器响应中 json 中的斜杠来自哪里,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53925906/

相关文章:

python turtle goto 不允许负 float

java - Spring Boot 中的 @PathVariable 在 URL 中带有斜杠

java - Jersey Client API - 使用 jersey rest api 的身份验证错误

json - json到对象的转换期间发生异常:无法解析'javaTypes'中的'json__TypeId__'

json - Grails 2.5.0 静态编译、 Controller 和 grails 功能

asp.net - System.Json.DLL 在哪里?

javascript - 为什么我的 JSON 返回单引号(撇号)的转义码

python - 如何让脚本等待用户在 Tkinter 中交互?

android - 改造未正确接收响应

python - Django REST框架: raise error when extra fields are present on POST