Parsing JSON giving “unexpected token o” error \[duplicate\]

JavaScript

猿飞云斯丁

2020-03-14

I am having a problem parsing simple JSON strings. I have checked them on JSONLint and it shows that they are valid. But when I try to parse them using either JSON.parse or the jQuery alternative it gives me the error unexpected token o:

<!doctype HTML>
<html>
  <head>
  </head>
  <body>
    <script type="text/javascript">
      var cur_ques_details ={"ques_id":15,"ques_title":"jlkjlkjlkjljl"};
      var ques_list = JSON.parse(cur_ques_details);

      document.write(ques_list['ques_title']);
    </script>
  </body>
</html>

Note: I'm encoding my strings using json_encode() in PHP.

第1582篇《Parsing JSON giving “unexpected token o” error \[duplicate\]》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

2个回答
理查德路易 2020.03.14

Try parse so:

var yourval = jQuery.parseJSON(JSON.stringify(data));
达蒙乐 2020.03.14

Using JSON.stringify(data);:

$.ajax({
    url: ...
    success:function(data){
        JSON.stringify(data); //to string
        alert(data.you_value); //to view you pop up
    }
});

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android