是什么导致了第三行上的错误?
Var product = [{ “名称”:“披萨”, “价格”:“10”, “数量”:“7” },{ “名称”:“Cerveja”, “价格”:“12”, “数量”:“5” },{ “名称”:“汉堡”, “价格”:“10”, “数量”:“2” },{ “名称”:“Fraldas”, “价格”:“6”, “数量”:“2” }); console.log(产品); var b = JSON.parse(products);//意外令牌o
打开控制台以查看错误
是什么导致了第三行上的错误?
Var product = [{ “名称”:“披萨”, “价格”:“10”, “数量”:“7” },{ “名称”:“Cerveja”, “价格”:“12”, “数量”:“5” },{ “名称”:“汉堡”, “价格”:“10”, “数量”:“2” },{ “名称”:“Fraldas”, “价格”:“6”, “数量”:“2” }); console.log(产品); var b = JSON.parse(products);//意外令牌o
打开控制台以查看错误
当前回答
Products是一个对象。(从对象字面量创建)
JSON.parse()用于将包含JSON符号的字符串转换为Javascript对象。
您的代码将对象转换为字符串(通过调用. tostring()),以便尝试将其解析为JSON文本。 默认的.toString()返回"[object object]",这不是有效的JSON;因此出现了错误。
其他回答
哦,天哪,之前所有答案中的解决方案对我都不起作用。我刚才也遇到了类似的问题。我设法解决了它与包装与报价。请看截图。喔!
原:
Var product = [{ “名称”:“披萨”, “价格”:“10”, “数量”:“7” },{ “名称”:“Cerveja”, “价格”:“12”, “数量”:“5” },{ “名称”:“汉堡”, “价格”:“10”, “数量”:“2” },{ “名称”:“Fraldas”, “价格”:“6”, “数量”:“2” }); console.log(产品); var b = JSON.parse(products);//意外令牌o
这是我根据以前的回复做的一个函数:它在我的机器上工作,但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 characters
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;
}
如果有前导空格或尾随空格,则无效。 后面和前面的空格可以被删除为
mystring = mystring.replace(/^\s+|\s+$/g, "");
删除字符串的前导或尾随空格
您应该在这里验证JSON字符串。
一个有效的JSON字符串必须在键周围有双引号:
JSON.parse({"u1":1000,"u2":1100}) // will be ok
如果没有引号,它将导致一个错误:
JSON.parse({u1:1000,u2:1100})
// error Uncaught SyntaxError: Unexpected token u in JSON at position 2
使用单引号也会导致错误:
JSON.parse({'u1':1000,'u2':1100})
// error Uncaught SyntaxError: Unexpected token ' in JSON at position 1
现在,\r、\b、\t、\f等显然不是唯一会给你这个错误的有问题的字符。
注意,有些浏览器可能对JSON.parse的输入有额外的要求。
在浏览器中运行测试代码:
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);
在Chrome上测试,我看到它不允许JSON.parse(String.fromCharCode(0x22, x, 0x22));其中x是34 92,或者从0到31。
字符34和92分别是"和\字符,它们通常是预期字符和正确转义字符。0到31个字符会给你带来问题。
为了帮助调试,在执行JSON.parse(input)之前,首先验证输入不包含有问题的字符:
function VerifyInput(input){
for(var x=0; x<input.length; ++x){
let c = input.charCodeAt(x);
if(c >= 0 && c <= 31){
throw 'problematic character found at position ' + x;
}
}
}