2

在主函数中,我调用了一个调用另一个应用程序的函数,结果我得到了 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.
4

1 回答 1

1

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

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

JSON.parse(response);

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

from django.http import JsonResponse
return JsonResponse(json)
于 2018-12-26T05:40:47.047 回答