解析 JSON 给出“意外令牌 o”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15617164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parsing JSON giving "unexpected token o" error
提问by Saurabh Sharma
I am having a problem parsing simple JSON strings. I have checked them on JSONLintand it shows that they are valid. But when I try to parse them using either JSON.parseor the jQuery alternative it gives me the error unexpected token o:
我在解析简单的 JSON 字符串时遇到问题。我已经在JSONLint上检查过它们,它表明它们是有效的。但是当我尝试使用JSON.parsejQuery 或 jQuery 替代方法解析它们时,它给了我错误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.
注意:我正在使用json_encode()PHP编码我的字符串。
回答by Dark Falcon
Your data is already an object. No need to parse it. The javascript interpreter has already parsed it for you.
您的数据已经是一个对象。不需要解析它。javascript 解释器已经为您解析了它。
var cur_ques_details ={"ques_id":15,"ques_title":"jlkjlkjlkjljl"};
document.write(cur_ques_details['ques_title']);
回答by Юрий Шпакович
Try parse so:
尝试解析:
var yourval = jQuery.parseJSON(JSON.stringify(data));
回答by Iyes boy
Using JSON.stringify(data);:
使用JSON.stringify(data);:
$.ajax({
url: ...
success:function(data){
JSON.stringify(data); //to string
alert(data.you_value); //to view you pop up
}
});
回答by PlantationGator
The source of your error, however, is that you need to place the full JSON string in quotes. The following will fix your sample:
但是,错误的根源在于您需要将完整的 JSON 字符串放在引号中。以下将修复您的示例:
<!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>
As the other respondents have mentioned, the object is already parsed into a JS object so you don't need to parse it. To demonstrate how to accomplish the same thing without parsing, you can do the following:
正如其他受访者所提到的,该对象已经解析为 JS 对象,因此您无需解析它。要演示如何在不解析的情况下完成相同的事情,您可以执行以下操作:
<!doctype HTML>
<html>
<head>
</head>
<body>
<script type="text/javascript">
var cur_ques_details ={"ques_id":"15","ques_title":"jlkjlkjlkjljl"};
document.write(cur_ques_details.ques_title);
</script>
</body>
</html>
回答by Shuping
cur_ques_detailsis already a JS object, you don't need to parse it
cur_ques_details已经是JS对象,不需要解析
回答by Muhammad Soliman
Response is already parsed, you don't need to parse it again. if you parse it again it will give you "unexpected token o". if you need to get it as string, you could use JSON.stringify()
响应已经解析,您不需要再次解析它。如果你再次解析它,它会给你“ unexpected token o”。如果您需要将其作为字符串获取,则可以使用JSON.stringify()
回答by Saber
I had the same problem when I submitted data using jQuery AJAX:
我在使用 jQuery AJAX 提交数据时遇到了同样的问题:
$.ajax({
url:...
success:function(data){
//server response's data is JSON
//I use jQuery's parseJSON method
$.parseJSON(data);//it's ERROR
}
});
If the response is JSON, and you use this method, the data you get is a JavaScript object, but if you use dataType:"text", data is a JSON string. Then the use of $.parseJSONis okay.
如果响应是 JSON,并且您使用此方法,则您获得的数据是一个 JavaScript 对象,但如果您使用dataType:"text",则数据是一个 JSON 字符串。那么使用$.parseJSON就OK了。
回答by alexkb
I was seeing this unexpected token oerror because my (incomplete) code had run previously (live reload!) and set the particular keyed local storage value to [object Object]instead of {}. It wasn't until I changed keys, that things started working as expected. Alternatively, you can follow these instructions to delete the incorrectly set localStorage value.
我看到这个unexpected token o错误是因为我的(不完整的)代码之前运行过(实时重新加载!)并将特定的键控本地存储值设置[object Object]为{}. 直到我更改了密钥,事情才开始按预期工作。或者,您可以按照这些说明删除错误设置的 localStorage 值。

