[英]Uncaught SyntaxError: Unexpected token with JSON.parse
是什么导致第三行出现此错误?
打开控制台查看错误<\/sup>
products<\/code>是一个对象。
(从对象字面量创建)
JSON.parse()<\/code>用于将包含 JSON 表示法的字符串<\/strong>转换为 Javascript 对象。
您的代码将对象转换为字符串(通过调用
.toString()<\/code> ),以便尝试将其解析为 JSON 文本。
默认的
.toString()<\/code>返回
"[object Object]"<\/code> ,这是无效的 JSON;
因此错误。
假设你知道它是有效的 JSON,但你仍然得到这个......
在这种情况下,字符串中可能有隐藏\/特殊字符来自您获取它们的任何来源。 当您粘贴到验证器中时,它们会丢失 - 但在字符串中它们仍然存在。 这些字符虽然不可见,但会破坏
JSON.parse()<\/code>
如果
s<\/code>是您的原始 JSON,则使用以下命令对其进行清理:
// preserve newlines, etc - use valid JSON
s = s.replace(/\\n/g, "\\n")
.replace(/\\'/g, "\\'")
.replace(/\\"/g, '\\"')
.replace(/\\&/g, "\\&")
.replace(/\\r/g, "\\r")
.replace(/\\t/g, "\\t")
.replace(/\\b/g, "\\b")
.replace(/\\f/g, "\\f");
// remove non-printable and other non-valid JSON chars
s = s.replace(/[\u0000-\u0019]+/g,"");
var o = JSON.parse(s);
似乎您想对对象进行字符串化<\/strong>,而不是解析。 所以这样做:
JSON.stringify(products);
我发现与
JSON.parse(inputString)<\/code>相同的问题。
在我的情况下,输入字符串来自我的服务器页面[return of a page method]<\/em> 。
我打印了
typeof(inputString)<\/code> - 它是字符串,仍然发生错误。
我也试过
JSON.stringify(inputString)<\/code> ,但没有帮助。
后来我发现这是字段值内的换行符
[\\n]<\/code>的问题。
我做了一个替换[用其他字符,在解析后放回新行]<\/em> ,一切正常。
JSON.parse 正在等待参数中的字符串。 您需要对 JSON 对象进行字符串化以解决问题。
products = [{"name":"Pizza","price":"10","quantity":"7"}, {"name":"Cerveja","price":"12","quantity":"5"}, {"name":"Hamburguer","price":"10","quantity":"2"}, {"name":"Fraldas","price":"6","quantity":"2"}];
console.log(products);
var b = JSON.parse(JSON.stringify(products)); //solves the problem
products = [{"name":"Pizza","price":"10","quantity":"7"}, {"name":"Cerveja","price":"12","quantity":"5"}, {"name":"Hamburguer","price":"10","quantity":"2"}, {"name":"Fraldas","price":"6","quantity":"2"}];
如果有前导或尾随空格,则无效。 尾随\/前导空格可以删除为
mystring = mystring.replace(/^\s+|\s+$/g, "");
这是我根据以前的回复制作的一个功能:它可以在我的机器上运行,但是 YMMV。
/**
* @description Converts a string response to an array of objects.
* @param {string} string - The string you want to convert.
* @returns {array} - an array of objects.
*/
function stringToJson(input) {
var result = [];
//replace leading and trailing [], if present
input = input.replace(/^\[/,'');
input = input.replace(/\]$/,'');
//change the delimiter to
input = input.replace(/},{/g,'};;;{');
// preserve newlines, etc - use valid JSON
//https://stackoverflow.com/questions/14432165/uncaught-syntaxerror-unexpected-token-with-json-parse
input = input.replace(/\\n/g, "\\n")
.replace(/\\'/g, "\\'")
.replace(/\\"/g, '\\"')
.replace(/\\&/g, "\\&")
.replace(/\\r/g, "\\r")
.replace(/\\t/g, "\\t")
.replace(/\\b/g, "\\b")
.replace(/\\f/g, "\\f");
// remove non-printable and other non-valid JSON chars
input = input.replace(/[\u0000-\u0019]+/g,"");
input = input.split(';;;');
input.forEach(function(element) {
// console.log(JSON.stringify(element));
result.push(JSON.parse(element));
}, this);
return result;
}
调用
JSON.parse()<\/code>时可能导致
"SyntaxError: Unexpected token"<\/code>异常的另一个问题是在字符串值中使用以下任何内容:
换行符。
选项卡(是的,您可以使用 Tab 键生成的选项卡!)
任何独立的斜线
\\<\/code> (但由于某种原因不是
\/<\/code> ,至少不是在 Chrome 上。)
例如,以下内容将为您提供此异常:
所以应该改为:我应该说,这使得它在带有大量文本的纯 JSON 格式中非常不可读。"
[
{
"name": "Pizza",
"price": "10",
"quantity": "7"
},
{
"name": "Cerveja",
"price": "12",
"quantity": "5"
},
{
"name": "Hamburguer",
"price": "10",
"quantity": "2"
},
{
"name": "Fraldas",
"price": "6",
"quantity": "2"
}
]
希望这对其他人有帮助。
我的问题是我通过 AJAX 在 PHP 回调函数中对 HTML 进行了注释,该函数正在解析注释并返回无效的 JSON。
一旦我删除了注释的 HTML,一切都很好,JSON 被解析没有问题。
当您使用 POST 或 PUT 方法时,请确保对正文部分进行字符串化。
我在这里记录了一个示例https:\/\/gist.github.com\/manju16832003\/4a92a2be693a8fda7ca84b58b8fa7154<\/a>
您正在做的唯一错误是,您正在解析已经解析的对象,所以它会抛出错误,使用它,您会很高兴。
如果要打印整个 json,请使用 JSON.stringify()
products 是一个可以直接使用的数组:
var i, j;
for(i=0;i<products.length;i++)
for(j in products[i])
console.log("property name: " + j,"value: "+products[i][j]);
现在显然
\\r<\/code> ,
\\b<\/code> ,
\\t<\/code> ,
\\f<\/code>等不是唯一可以给你这个错误的有问题的字符。
请注意,某些浏览器可能对
JSON.parse<\/code>的输入有额外<\/em>的要求。
在浏览器上运行此测试代码:
var arr = [];
for(var x=0; x < 0xffff; ++x){
try{
JSON.parse(String.fromCharCode(0x22, x, 0x22));
}catch(e){
arr.push(x);
}
}
console.log(arr);
哦,伙计,到目前为止提供的所有上述答案中的解决方案都对我不起作用。 我刚才也有类似的问题。 我设法用引号来解决它。 请参阅屏幕截图。 哇。
原来的:<\/strong>
您得到的错误,即“意外的令牌 o”是因为 json 是预期的,但在解析时获取了对象。 那个“o”是单词“object”的第一个字母。
"发生这种情况的原因有很多,但可能是因为字符无效,因此您可以使用
JSON.stringify(obj);<\/code>
这会将您的对象转换为 JSON,但请记住它是 JQUERY 表达式。
"
我有这个错误,因为返回json对象的API给出了一个错误(在我的情况下Code Igniter,当php代码失败时返回一个html)所以它不是JSON OBJECT。
检查SQL Sentences和PHP代码,并使用Postman(或其他一些API测试人员)进行测试
在我的情况下,我的
JSON<\/code>字符串中存在以下字符问题
\\n<\/li>
:<\/li>
"<\/li><\/ol> 我已将它们替换为其他字符或符号,然后再次从编码中恢复。
"
现在这是一个 JavaScript 对象数组,而不是 JSON 格式。 要将其转换为 JSON 格式,您需要使用名为JSON.stringify()<\/strong>的函数
JSON.stringify(products)
为什么需要 JSON.parse? 它已经在对象格式的数组中。
更好地使用 JSON.stringify 如下:
var b = JSON.stringify(products);<\/code>
这可能会对您有所帮助。
我正在做的错误是将
null<\/code> (在不知不觉中)传递给 JSON.parse()。
所以它
Unexpected token n in JSON at position 0<\/code>
但是,每当您在 JSON.parse() 中传递不是 JS 对象的东西时,就会发生这种情况
是什么导致第三行出现此错误?
var products = [{ "name": "Pizza", "price": "10", "quantity": "7" }, { "name": "Cerveja", "price": "12", "quantity": "5" }, { "name": "Hamburguer", "price": "10", "quantity": "2" }, { "name": "Fraldas", "price": "6", "quantity": "2" }]; console.log(products); var b = JSON.parse(products); //unexpected token o
打开控制台以查看错误
是什么导致第三行出现此错误?
var products = [{ "name": "Pizza", "price": "10", "quantity": "7" }, { "name": "Cerveja", "price": "12", "quantity": "5" }, { "name": "Hamburguer", "price": "10", "quantity": "2" }, { "name": "Fraldas", "price": "6", "quantity": "2" }]; console.log(products); var b = JSON.parse(products); //unexpected token o
打开控制台以查看错误
使用
eval<\/code> 。
它将 JavaScript 表达式\/代码作为字符串并评估\/执行它。
eval(inputString);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.