用单引号将字符串解析为JSON ?

时间:2022-10-28 15:05:59

I have a string

我有一个字符串

str = "{'a':1}";
JSON.parse(str);
VM514:1 Uncaught SyntaxError: Unexpected token '(…)

How can I parse above string (str) to JSON ? This seems like a simple parsing; it's not working somehow.

如何将上面的字符串(str)解析为JSON ?这似乎是一个简单的解析;这不是工作。

6 个解决方案

#1


32  

The JSON standard requires double quotes and will not accept single quotes, nor will the parser.

JSON标准要求双引号,不接受单引号,解析器也不接受单引号。

If you have a simple case with no escaped single quotes in your strings (which would normally be impossible, but this isn't JSON), you can simple str.replace(/'/g, '"') and you should end up with valid JSON.

如果您的字符串中没有转义单引号(这通常是不可能的,但这不是JSON),那么您可以使用简单的string .replace(/'/g, ' ' ' '),您应该使用有效的JSON。

#2


5  

Using single quotes for keys are not allowed in JSON. You need to use double quotes.

JSON中不允许对键使用单引号。你需要使用双引号。

For your use-case perhaps this would be the easiest solution:

对于您的用例,这可能是最简单的解决方案:

str = '{"a":1}';

Source:

来源:

If a property requires quotes, double quotes must be used. All property names must be surrounded by double quotes.

如果属性需要引号,则必须使用双引号。所有属性名必须用双引号括起来。

#3


2  

Something like this:

是这样的:

var div = document.getElementById("result");

var str = "{'a':1}";
  str = str.replace(/\'/g, '"');
  var parsed = JSON.parse(str);
  console.log(parsed);
  div.innerText = parsed.a;
<div id="result"></div>

#4


0  

If you are sure your JSON is safely under your control (not user input) then you can simply evaluate the JSON. Eval accepts all quote types as well as unquoted property names.

如果您确信您的JSON在您的控制下是安全的(而不是用户输入),那么您只需对JSON进行评估。Eval接受所有引用类型以及未引用的属性名。

var str = "{'a':1}";
var myObject = eval('(' + str + ')');

The extra parentheses are required due to how the eval parser works. Eval is not evil when it is used on data you have control over. For more on the difference between JSON.parse and eval() see JSON.parse vs. eval()

由于eval解析器的工作方式,需要额外的括号。当Eval把它用于您可以控制的数据时,它就不是邪恶的了。有关JSON之间的差异的更多信息。解析和eval()请参见JSON。解析与eval()

#5


0  

I know it's an old post, but you can use JSON5 for this purpose.

我知道这是一个旧的帖子,但是您可以为此目的使用JSON5。

<script src="json5.js"></script>
<script>JSON.stringify(JSON5.parse('{a:1}'))</script>

#6


-1  

json = ( new Function("return " + jsonString) )(); 

#1


32  

The JSON standard requires double quotes and will not accept single quotes, nor will the parser.

JSON标准要求双引号,不接受单引号,解析器也不接受单引号。

If you have a simple case with no escaped single quotes in your strings (which would normally be impossible, but this isn't JSON), you can simple str.replace(/'/g, '"') and you should end up with valid JSON.

如果您的字符串中没有转义单引号(这通常是不可能的,但这不是JSON),那么您可以使用简单的string .replace(/'/g, ' ' ' '),您应该使用有效的JSON。

#2


5  

Using single quotes for keys are not allowed in JSON. You need to use double quotes.

JSON中不允许对键使用单引号。你需要使用双引号。

For your use-case perhaps this would be the easiest solution:

对于您的用例,这可能是最简单的解决方案:

str = '{"a":1}';

Source:

来源:

If a property requires quotes, double quotes must be used. All property names must be surrounded by double quotes.

如果属性需要引号,则必须使用双引号。所有属性名必须用双引号括起来。

#3


2  

Something like this:

是这样的:

var div = document.getElementById("result");

var str = "{'a':1}";
  str = str.replace(/\'/g, '"');
  var parsed = JSON.parse(str);
  console.log(parsed);
  div.innerText = parsed.a;
<div id="result"></div>

#4


0  

If you are sure your JSON is safely under your control (not user input) then you can simply evaluate the JSON. Eval accepts all quote types as well as unquoted property names.

如果您确信您的JSON在您的控制下是安全的(而不是用户输入),那么您只需对JSON进行评估。Eval接受所有引用类型以及未引用的属性名。

var str = "{'a':1}";
var myObject = eval('(' + str + ')');

The extra parentheses are required due to how the eval parser works. Eval is not evil when it is used on data you have control over. For more on the difference between JSON.parse and eval() see JSON.parse vs. eval()

由于eval解析器的工作方式,需要额外的括号。当Eval把它用于您可以控制的数据时,它就不是邪恶的了。有关JSON之间的差异的更多信息。解析和eval()请参见JSON。解析与eval()

#5


0  

I know it's an old post, but you can use JSON5 for this purpose.

我知道这是一个旧的帖子,但是您可以为此目的使用JSON5。

<script src="json5.js"></script>
<script>JSON.stringify(JSON5.parse('{a:1}'))</script>

#6


-1  

json = ( new Function("return " + jsonString) )();