【问题标题】:Shodan JSON from python to javascript issueShodan JSON 从 python 到 javascript 问题
【发布时间】:2013-01-23 08:03:44
【问题描述】:

我正在使用 Shodan API,但在将收到的 JSON 传递给模板 (django) 时遇到问题。

这是原始 JSON 结构:

{"matches": [{"city": "Montreal", "updated": "02.02.2013", "ip": "24.48.3.143", "longitude": -73.5833, "data": "HTTP/1.0 200 OK\r\nDate: Sat, 02 Feb 2013 00:41:53 GMT\r\nServer: Apache/1.3.42 (Unix) mod_auth_pam/1.1.1 DAV/1.0.3 mod_ssl/2.8.31 OpenSSL/0.9.8g\r\nSet-Cookie: iomega=174.79.246.153.26931359765713476; path=/\r\nCache-Control: no-cache\r\nConnection: close\r\nTransfer-Encoding: chunked\r\nContent-Type: text/html; charset=utf-8\r\n\r\n", "country_name": "Canada", "hostnames": [], "country_code": "CA", "country": "DEPRECATED: use country_name", "latitude": 45.5, "os": "Linux 2.6.x", "port": 443}]}

这是我传递给模板的数据结构:

[{"city": "Montreal", "updated": "02.02.2013", "os": "Linux 2.6.x", "ip": "24.48.3.143", "longitude": -73.5833, "latitude": 45.5, "hostnames": [], "country_code": "CA", "country": "DEPRECATED: use country_name", "country_name": "Canada", "data": "HTTP/1.0 200 OK\r\nDate: Sat, 02 Feb 2013 00:41:53 GMT\r\nServer: Apache/1.3.42 (Unix) mod_auth_pam/1.1.1 DAV/1.0.3 mod_ssl/2.8.31 OpenSSL/0.9.8g\r\nSet-Cookie: iomega=174.79.246.153.26931359765713476; path=/\r\nCache-Control: no-cache\r\nConnection: close\r\nTransfer-Encoding: chunked\r\nContent-Type: text/html; charset=utf-8\r\n\r\n", "port": 443}]

正如你所看到的,它里面有很多特殊的字符,它们会扰乱转换。另一个问题是“主机名”子元素,它是元素列表(如果存在元素)......

有人知道如何以正确的格式处理 js/jquery 中的这个 JSON 文件吗? 问候。

编辑:我正在检查我的 javascript 的输出,并且从 django 视图传递到模板的特殊字符被转换为 html 代码,\r\n 消失导致解析错误:

我比较了两个 json 文件:http://json.parser.online.fr/

从 shodan api 接收并在视图中操作的 json 工作正常,但不是传递到模板的 JSON。

【问题讨论】:

    标签: javascript jquery python django json


    【解决方案1】:

    我发现了问题,正如我在问题中所说,当我将 JSON 从视图传递到模板时,django 会转换 JavaScript 字符串中使用的字符。

    解决方法是在模板中给你的变量添加escapejs标签:

    {{ JSON|escapejs}}

    您可以在此处找到文档:https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#escapejs

    【讨论】:

      猜你喜欢
      • 2021-03-26
      • 1970-01-01
      • 1970-01-01
      • 2018-02-21
      • 2011-10-24
      • 1970-01-01
      • 1970-01-01
      • 2011-11-09
      • 2011-04-12
      相关资源
      最近更新 更多