Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing JSON giving "unexpected token o" error [duplicate]

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.

like image 560
Saurabh Sharma Avatar asked Mar 25 '13 14:03

Saurabh Sharma


3 Answers

Your data is already an object. No need to parse it. The javascript interpreter has already parsed it for you.

var cur_ques_details ={"ques_id":15,"ques_title":"jlkjlkjlkjljl"};
document.write(cur_ques_details['ques_title']);
like image 172
Dark Falcon Avatar answered Oct 05 '22 01:10

Dark Falcon


Try parse so:

var yourval = jQuery.parseJSON(JSON.stringify(data));
like image 20
Юрий Шпакович Avatar answered Oct 05 '22 03:10

Юрий Шпакович


Using JSON.stringify(data);:

$.ajax({
    url: ...
    success:function(data){
        JSON.stringify(data); //to string
        alert(data.you_value); //to view you pop up
    }
});
like image 42
Iyes boy Avatar answered Oct 05 '22 01:10

Iyes boy