JSON.Parse,'Uncaught SyntaxError: Unexpected token o
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19239217/
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
JSON.Parse,'Uncaught SyntaxError: Unexpected token o
提问by Matthew David Jankowski
I am having trouble with JSON returned from a web service. It looks like the JSON lacks quotes, but when I add quotes to the JSON, I get an error. Here is the error message: 'Uncaught SyntaxError: Unexpected token o. When I log the string to console:[object Object],[object Object]
我在处理从 Web 服务返回的 JSON 时遇到问题。看起来 JSON 缺少引号,但是当我向 JSON 添加引号时,出现错误。这是错误消息:'Uncaught SyntaxError: Unexpected token o. 当我将字符串记录到控制台时:[object Object],[object Object]
Here is some example code that simulates the error:
下面是一些模拟错误的示例代码:
//Error I am trying to solve
var jsonString = '[{"Id":"10","Name":"Matt"},{"Id":"1","Name":"Rock"}]';
var myData = JSON.parse(jsonString);
$(document).ready(function() {
var $grouplist = $('#groups');
$.each(myData, function() {
$('<li>' + this.Name + '</li>').appendTo($grouplist);
});
});
Here is the same code with the single quotes around the string. It works
这是相同的代码,字符串周围有单引号。有用
//Successful Javascript
var jsonString = '[{"Id":"10","Name":"Matt"},{"Id":"1","Name":"Rock"}]';
var myData = JSON.parse(jsonString);
$(document).ready(function() {
var $grouplist = $('#groups');
$.each(myData, function() {
$('<li>' + this.Name + '</li>').appendTo($grouplist);
});
});
//Successful HTML
<ul id="groups"></ul>
But when I try to add quotes to the string, like I seem to need to in my real code, it fails:
但是当我尝试向字符串添加引号时,就像我在实际代码中似乎需要的那样,它失败了:
//Does not work when I need to append quotes to the string:
var jsonStringNoQuotes = [{"Id":"10","Name":"Matt"},{"Id":"1","Name":"Rock"}];
jsonStringQuotes = "'" + jsonStringNoQuotes + "'";
var myData = JSON.parse(jsonStringQuotes);
$(document).ready(function() {
var $grouplist = $('#groups');
$.each(myData, function() {
$('<li>' + this.Name + ',' + this.Id + '</li>').appendTo($grouplist);
});
});
Here is the error: log string to console:[object Object],[object Object] data.js:809 Uncaught SyntaxError: Unexpected token '
这是错误:将字符串记录到控制台:[object Object],[object Object] data.js:809 Uncaught SyntaxError: Unexpected token '
I'm stumped. Any help appreciated! Thank you!
我难住了。任何帮助表示赞赏!谢谢!
回答by flagoworld
Without single quotes around it, you are creating an array with two objects inside of it. This is JavaScript's own syntax. When you add the quotes, that object (array+2 objects) is now a string. You can use JSON.parseto convert a string into a JavaScript object. You cannot use JSON.parseto convert a JavaScript object into a JavaScript object.
如果没有单引号,您将创建一个包含两个对象的数组。这是 JavaScript 自己的语法。当您添加引号时,该对象(数组 + 2 个对象)现在是一个字符串。您可以使用JSON.parse将字符串转换为 JavaScript 对象。您不能使用JSON.parse将 JavaScript 对象转换为 JavaScript 对象。
//String - you can use JSON.parse on it
var jsonStringNoQuotes = '[{"Id":"10","Name":"Matt"},{"Id":"1","Name":"Rock"}]';
//Already a javascript object - you cannot use JSON.parse on it
var jsonStringNoQuotes = [{"Id":"10","Name":"Matt"},{"Id":"1","Name":"Rock"}];
Furthermore, your last example fails because you are adding literal single quote characters to the JSON string. This is illegal. JSON specification states that only double quotes are allowed. If you were to console.logthe following...
此外,您的最后一个示例失败了,因为您向 JSON 字符串添加了文字单引号字符。这是非法的。JSON 规范规定只允许使用双引号。如果你console.log对以下...
console.log("'"+[{"Id":"10","Name":"Matt"},{"Id":"1","Name":"Rock"}]+"'");
//Logs:
'[object Object],[object Object]'
You would see that it returns the string representation of the array, which gets converted to a comma separated list, and each list item would be the string representation of an object, which is [object Object]. Remember, associative arrays in javascript are simply objects with each key/value pair being a property/value.
你会看到它返回数组的字符串表示,它被转换为逗号分隔的列表,每个列表项都是一个对象的字符串表示,即[object Object]. 请记住,javascript 中的关联数组只是对象,每个键/值对都是一个属性/值。
Why does this happen? Because you are starting with a string "'", then you are trying to append the array to it, which requests the string representation of it, then you are appending another string "'".
为什么会发生这种情况?因为您从 string 开始"'",然后您尝试将数组附加到它,它请求它的字符串表示形式,然后您附加另一个 string "'"。
Please do not confuse JSON with Javascript, as they are entirely different things. JSON is a data format that is humanly readable, and was intended to match the syntax used when creating javascript objects. JSON is a string. Javascript objects are not, and therefor when declared in code are not surrounded in quotes.
请不要将 JSON 与 Javascript 混淆,因为它们是完全不同的东西。JSON 是一种人类可读的数据格式,旨在匹配创建 javascript 对象时使用的语法。JSON 是一个字符串。Javascript 对象不是,因此在代码中声明时不会用引号括起来。
See this fiddle: http://jsfiddle.net/NrnK5/
看到这个小提琴:http: //jsfiddle.net/NrnK5/
回答by Pankaj Sharma
var jsonStringNoQuotes = [{"Id":"10","Name":"Matt"},{"Id":"1","Name":"Rock"}];
it will create json object. no need to parse.
它将创建 json 对象。无需解析。
jsonStringQuotes = "'" + jsonStringNoQuotes + "'";
will return '[object]'
将返回“[对象]”
thats why it(below) is causing error
这就是为什么它(下面)导致错误
var myData = JSON.parse(jsonStringQuotes);
回答by volpav
Maybe what comes from the server is already evaluated as JSON object? For example, using jQuery getmethod:
也许来自服务器的东西已经被评估为 JSON 对象?例如,使用jQuery get方法:
$.get('/service', function(data) {
var obj = data;
/*
"obj" is evaluated at this point if server responded
with "application/json" or similar.
*/
for (var i = 0; i < obj.length; i++) {
console.log(obj[i].Name);
}
});
Alternatively, if you need to turn JSON object into JSON string literal, you can use JSON.stringify:
或者,如果您需要将 JSON 对象转换为 JSON 字符串文字,您可以使用JSON.stringify:
var json = [{"Id":"10","Name":"Matt"},{"Id":"1","Name":"Rock"}];
var jsonString = JSON.stringify(json);
But in this case I don't understand why you can't just take the jsonvariable and refer to it instead of stringifying and parsing.
但在这种情况下,我不明白为什么你不能只获取json变量并引用它而不是字符串化和解析。
回答by Stephen Rosenthal
Your last example is invalid JSON. Single quotes are not allowed in JSON except inside strings. In the second example, the single quotes are not in the string, but serve to show the start and end.
您的最后一个示例是无效的 JSON。除了字符串内部,JSON 中不允许使用单引号。在第二个示例中,单引号不在字符串中,而是用于显示开始和结束。
See http://www.json.org/for the specifications.
有关规范,请参阅http://www.json.org/。
Should add: Why do you think this: "like I seem to need to in my real code"? Then maybe we can help you come up with the solution.
应该补充:你为什么这么认为:“就像我在我的真实代码中似乎需要这样”?那么也许我们可以帮助您提出解决方案。

