简体   繁体   English

使用JSON.parse时,出现“ SyntaxError:JSON中位置1的意外令牌”

[英]I am getting “SyntaxError: Unexpected token ' in JSON at position 1” when using JSON.parse

I'm having trouble accessing the list param = ['foo','bar'] on myView.js . 我在访问myView.js上的列表param = ['foo','bar']myView.js

the JS code is treating param as if it were a string, but when I try JSON.parse(param) I get the error shown below. JS代码将param当作字符串对待,但是当我尝试JSON.parse(param) ,出现如下所示的错误。

What am I missing here? 我在这里想念什么?

myView.html: myView.html:

    <script>
    window.obj = {};
    obj.param = "{{ param | safe }}";
    </script>

views.py: views.py:

    def myView(req):
        context = {'param':['foo','bar']}
        return render(req, 'myView.html', context)

myView.js: myView.js:

$(document).ready(function() {

      console.log(window.obj.param); //prints ['foo','bar']
      console.log(window.obj.param[0]); //prints [
      console.log(JSON.parse(window.obj.param)); // Uncaught SyntaxError: Unexpected token ' in JSON at position 1

      //what I need to happen
      console.log(param[0]); // prints 'foo'
});

json format allows only double quotes ( token ' in JSON at position 1 ), sting with single quotes is not a valid json format, so if you try to parse it, you will get an error, as demonstrated here json格式仅允许使用双引号( token ' in JSON at position 1 ),单引号的字符串不是有效的json格式,因此,如果您尝试解析它,则会收到错误,如此处所示

 function safeParse(param){ try { console.log(JSON.parse(param)) } catch (e) { console.log("there was an issue with parsing your string") } } safeParse("['foo','bar']"); safeParse('["foo","bar"]'); 

暂无
暂无

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

相关问题 我正在使用Ionic并收到错误消息:SyntaxError:JSON中的意外标记&lt;在JSON.parse位置0处( <anonymous> ) - I am using ionic and getting an error: SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>) 使用JSON.parse时,&#39;未捕获的SyntaxError:意外的标记u&#39; - 'Uncaught SyntaxError: Unexpected token u' when using JSON.parse 未捕获到的SyntaxError:JSON中的意外令牌u在JSON.parse的位置0 - Uncaught SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse 未捕获的SyntaxError:JSON.parse中位置0的JSON中的意外标记a( <anonymous> ) - Uncaught SyntaxError: Unexpected token a in JSON at position 0 at JSON.parse (<anonymous>) JSON.parse() 导致错误:`SyntaxError: Unexpected token in JSON at position 0` - JSON.parse() causes error: `SyntaxError: Unexpected token in JSON at position 0` JSON.parse(): SyntaxError: Unexpected token in JSON at position 0 - JSON.parse(): SyntaxError: Unexpected token � in JSON at position 0 JSON.parse() 错误 SyntaxError: Unexpected token &lt; in JSON at position 0 - JSON.parse() Error SyntaxError: Unexpected token < in JSON at position 0 语法错误:JSON 中的意外令牌 e 在 position 1 与 JSON.parse() - SyntaxError: Unexpected token e in JSON at position 1 with JSON.parse() SyntaxError:JSON中的意外令牌u在JSON.parse( <anonymous> ) - SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse (<anonymous>) 未捕获到的SyntaxError:JSON中的意外令牌&lt;在JSON.parse位置0处( <anonymous> ) - Uncaught SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM