未捕获的SyntaxError:具有JSON.parse的意外标记
是什么导致这个错误在第三行?
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 打开控制台查看错误
  products是一个对象。  (从对象文字创build) 
  JSON.parse()用于将包含JSON表示法的string转换为Javascript对象。 
 你的代码把对象变成一个string(通过调用.toString() ),以试图parsing它为JSON文本。 
 默认.toString()返回"[object Object]" ,这是无效的JSON; 因此错误。 
假设你知道这是有效的JSON,但你仍然得到这个…
 在这种情况下,可能从string中隐藏/特殊字符。 当你粘贴到validation器中时,它们会丢失 – 但是在string中它们仍然存在。 那些不可见的字符将打破JSON.parse() 
 如果s是您的原始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); 
 看来你想把对象串起来 。 
 所以,你应该使用: 
 JSON.stringify(products); 
 错误的原因是JSON.parse()需要一个String值, products是一个Array 。 
  注意:我认为它试图json.parse('[object Array]') ,它抱怨它不希望在[ 。 
 我发现与JSON.parse(inputString)相同的问题。 
在我的情况下,inputstring来自我的服务器页面[返回一个页面方法] 。
 我打印的typeof(inputString) – 它是string,仍然发生错误。 
 我也尝试JSON.stringify(inputString) ,但它没有帮助。 
 后来我发现这是一个问题,新的行操作符[\n] ,在一个字段值。 
我做了replace[与其他字符,parsing后把新的一行] ,一切工作正常。
 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"}]; 
改成
 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"}]'; 
JSON.parse正在等待参数中的string。 你需要将你的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 
 [ { "name": "Pizza", "price": "10", "quantity": "7" }, { "name": "Cerveja", "price": "12", "quantity": "5" }, { "name": "Hamburguer", "price": "10", "quantity": "2" }, { "name": "Fraldas", "price": "6", "quantity": "2" } ] 
这是你可以parsing的完美Json。
当您使用POST或PUT方法时,请确保将身体部分串联起来。
我已经在这里logging了一个例子https://gist.github.com/manju16832003/4a92a2be693a8fda7ca84b58b8fa7154
 在调用JSON.parse()在string值中使用以下任一字符时,可能导致"SyntaxError: Unexpected token"exception的另一个问题是: 
- 
换行符。 
- 
标签(是的,您可以使用Tab键生成标签!) 
- 
任何独立的斜杠 \(但由于某种原因,不,/至less不是在Chrome上)。
(有关完整列表,请参阅此处的string部分 。)
例如下面的例子会给你带来这个例外:
 { "msg" : { "message": "It cannot contain a new-line", "description": "Some discription with a tabbed space is also bad", "value": "It cannot have 3\4 un-escaped" } } 
所以应该改成:
 { "msg" : { "message": "It cannot\ncontain a new-line", "description": "Some discription with a\t\ttabbed space", "value": "It cannot have 3\\4 un-escaped" } } 
我应该说,使用JSON格式的文本量更大,这使得它变得不可读。
希望这可以帮助别人。
我的问题是,我曾经通过AJAXparsing注释并返回无效的JSON,在PHPcallback函数中评论过HTML。
一旦我删除了评论的HTML,一切都很好,JSONparsing没有问题。
产品是一个可以直接使用的数组:
 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 , \b , \t , \f等不是唯一可以给你这个错误的问题字符。 
 请注意,某些浏览器可能对JSON.parse的input有额外的要求。 
在浏览器上运行这个testing代码:
 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上testing,我发现它不允许JSON.parse(String.fromCharCode(0x22, x, 0x22)); 其中x是34,92或0到31。 
 字符34和92分别是"和\字符,它们通常是预期的并且正确地逃脱了,字符为0到31会给你带来问题。 
 为了帮助debugging,在执行JSON.parse(input) ,首先确认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; } } } 
这是我在之前的回复中所做的一个function:它在我的机器上工作,但是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? 它已经在对象格式的数组中。
 更好地使用JSON.stringify如下: var b = JSON.stringify(products); 
这可能会帮助你。
 使用eval 。 它将JavaScriptexpression式/代码作为string并对其进行评估/执行。 
 eval(inputString);