解析 JSON 给出“意外标记 o"错误 [英] Parsing JSON giving "unexpected token o" error

查看:32
本文介绍了解析 JSON 给出“意外标记 o"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在解析简单的 JSON 字符串时遇到问题.我已经在 JSONLint 上检查过它们,它表明它们是有效的.但是当我尝试使用 JSON.parse 或 jQuery 替代品解析它们时,它给了我错误 unexpected token o:

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>

注意:我在 PHP 中使用 json_encode() 编码我的字符串.

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

推荐答案

您的数据已经是一个对象.不需要解析它.javascript 解释器已经为您解析过了.

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']);

这篇关于解析 JSON 给出“意外标记 o"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆