繁体   English   中英

Uncaught SyntaxError: Unexpected token & in JSON at position 1 at JSON.parse (<anonymous> )</anonymous>

[英]Uncaught SyntaxError: Unexpected token & in JSON at position 1 at JSON.parse (<anonymous>)

我正在尝试解析 JSON 但它一直在返回

Uncaught SyntaxError: Unexpected token & in JSON at position 1
    at JSON.parse (<anonymous>)

VS Code 中的代码如下所示:

var result = '{{ bans }}';
  var res = JSON.parse(result)

但在控制台中它看起来像:

var result = '{&#39;bans&#39;: [{&#39;appealable&#39;: False, &#39;banend&#39;: &#39;Fri, 15 Jan 2021 00:00:00 GMT&#39;, &#39;bannedby&#39;: &#39;parker02311&#39;, &#39;banstart&#39;: &#39;Fri, 15 Jan 2021 00:18:58 GMT&#39;, &#39;id&#39;: 1, &#39;images&#39;: &#39;https://cdn.discordapp.com/attachments/642127559600898058/799867505727504464/Roblox_1_15_2021_11_07_09_PM_2.png,https://cdn.discordapp.com/attachments/642127559600898058/799863972915314688/Roblox_1_15_2021_10_53_30_PM_2.png&#39;, &#39;notes&#39;: &#39;&lt;p&gt;test&lt;/p&gt;&#39;, &#39;reasons&#39;: &#39;Chat Bypassing, Discrimination&#39;, &#39;userid&#39;: &#39;17&#39;, &#39;username&#39;: &#39;test&#39;}, {&#39;appealable&#39;: False, &#39;banend&#39;: &#39;Wed, 20 Jan 2021 00:00:00 GMT&#39;, &#39;bannedby&#39;: &#39;parker02311&#39;, &#39;banstart&#39;: &#39;Sat, 16 Jan 2021 05:46:33 GMT&#39;, &#39;id&#39;: 2, &#39;images&#39;: &#39;https://image.prntscr.com/image/DUQMlOEKTuGDHSkvB4NuRQ.png,https://cdn.discordapp.com/attachments/642127559600898058/799874004457488394/unknown.png&#39;, &#39;notes&#39;: &#39;&#39;, &#39;reasons&#39;: &#39;Chat Bypassing,Discrimination&#39;, &#39;userid&#39;: &#39;202181894&#39;, &#39;username&#39;: &#39;Dagdoubleagaming&#39;}]}';
var res = JSON.parse(result)

我不确定所有 &#39 的来源,因为后端看起来像:

r = requests.get('http://localhost:5001/get/bans', timeout=5)

        if r.status_code == 200:
            jsonre = r.json()
            return render_template('index.html', name=current_user.username, title='Dashboard', breadcrums=True, bans=jsonre)

请求返回:

{
  "bans": [
    {
      "appealable": false, 
      "banend": "Fri, 15 Jan 2021 00:00:00 GMT", 
      "bannedby": "parker02311", 
      "banstart": "Fri, 15 Jan 2021 00:18:58 GMT", 
      "id": 1, 
      "images": "https://cdn.discordapp.com/attachments/642127559600898058/799867505727504464/Roblox_1_15_2021_11_07_09_PM_2.png,https://cdn.discordapp.com/attachments/642127559600898058/799863972915314688/Roblox_1_15_2021_10_53_30_PM_2.png", 
      "notes": "<p>test</p>", 
      "reasons": "Chat Bypassing, Discrimination", 
      "userid": "17", 
      "username": "test"
    }, 
    {
      "appealable": false, 
      "banend": "Wed, 20 Jan 2021 00:00:00 GMT", 
      "bannedby": "parker02311", 
      "banstart": "Sat, 16 Jan 2021 05:46:33 GMT", 
      "id": 2, 
      "images": "https://image.prntscr.com/image/DUQMlOEKTuGDHSkvB4NuRQ.png,https://cdn.discordapp.com/attachments/642127559600898058/799874004457488394/unknown.png", 
      "notes": "", 
      "reasons": "Chat Bypassing,Discrimination", 
      "userid": "202181894", 
      "username": "Dagdoubleagaming"
    }
  ]
}
var result = '{{ bans | escapejs }}'

只是在这里拍摄,但我看了一下这里“https://www.w3schools.com/Js/js_json_parse.asp”,我认为这可能有效。 我认为字符串不是 json 格式,这就是您收到错误的原因。

var 结果 = '{"bans"}'; var res = JSON.parse(结果)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM