JSON.parse:预期的属性名称或“}”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8013582/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:02:11  来源:igfitidea点击:

JSON.parse: expected property name or '}'

jsonparsing

提问by realnumber

Data contains (/"/):

数据包含("//):

 {"test":"101","mr":"103","bishop":"102"}

script:

脚本:

console.log($.parseJSON(result));

I'm getting error,

我收到错误,

JSON.parse: expected property name or '}'.

回答by alchemication

Had same issue when used single quotes in JSON file, changed to double quotes for all string properties/values and it's working OK now, hope it helps anyone....

在 JSON 文件中使用单引号时遇到同样的问题,所有字符串属性/值更改为双引号,现在工作正常,希望它可以帮助任何人......

Change:

改变:

JSON.parse("{'wrongQuotes': 5}") 

To:

到:

JSON.parse('{"rightQuotes": 5}')

回答by Jonathan M

If you're receiving the JSON with the encoded ", you'll have to replace each instance of "with a true "before doing JSON.parse. Something like:

如果您收到与编码的JSON ",你就必须更换的每个实例"有一个真正"做之前JSON.parse。就像是:

myJSONstring.replace(/"/ig,'"');

回答by kheengz

Change
{"test":"101","mr":"103","bishop":"102"}
To
'{"test":"101","mr":"103","bishop":"102"}'

更改
{"test":"101","mr":"103","bishop":"102"}

'{"test":"101","mr":"103","bishop":"102"}'

if this is coming from the server (PHP)
i.e <?php $php_var = ["test" => "101", "mr" => "103", "bishop" => "102"]?>

then on Javascriptend
var javascript_var = $.parseJSON('<?= json_encode($php_var) ?>');

如果这是来自服务器(PHP),
<?php $php_var = ["test" => "101", "mr" => "103", "bishop" => "102"]?>

Javascript
var javascript_var = $.parseJSON('<?= json_encode($php_var) ?>')

回答by Dazeh

For anyone who is using laravel blade and declaring a JS variable in a view.

对于使用 laravel 刀片并在视图中声明 JS 变量的任何人。

You need to use:

您需要使用:

var json = JSON.parse('{!! $json !!}');

Otherwise you will get this error due to the quotes being parsed as "

否则,由于引号被解析为“