Expect Object但在解析JSON时获取String

时间:2022-08-09 20:15:23

I am trying to parse a simple JSON string using jQuery

我试图使用jQuery解析一个简单的JSON字符串

var parsedJSON = $.parseJSON('{"graph_data": "{}"}');

I would expect typeof(parsedJSON.graph_data) to be an Object but instead it is returning string. What is the correct way to return an Object?

我希望typeof(parsedJSON.graph_data)是一个Object,但它返回的是字符串。返回Object的正确方法是什么?

2 个解决方案

#1


4  

It should be like

应该是这样的

var parsedJSON = $.parseJSON('{"graph_data": {}}');
console.log(typeof(parsedJSON.graph_data));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

no need of " for object, " is needed for defining string and object key. So it will treat as string here. For more about JSON structure and example visit http://www.json.org/.

定义字符串和对象键不需要“for object”。所以它会在这里视为字符串。有关JSON结构和示例的更多信息,请访问http://www.json.org/。

#2


0  

try it.

尝试一下。

var parsedJSON = $.parseJSON('{"graph_data": "{}"}'); console.log(parsedJSON.graph_data);

var parsedJSON = $ .parseJSON('{“graph_data”:“{}”}');的console.log(parsedJSON.graph_data);

#1


4  

It should be like

应该是这样的

var parsedJSON = $.parseJSON('{"graph_data": {}}');
console.log(typeof(parsedJSON.graph_data));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

no need of " for object, " is needed for defining string and object key. So it will treat as string here. For more about JSON structure and example visit http://www.json.org/.

定义字符串和对象键不需要“for object”。所以它会在这里视为字符串。有关JSON结构和示例的更多信息,请访问http://www.json.org/。

#2


0  

try it.

尝试一下。

var parsedJSON = $.parseJSON('{"graph_data": "{}"}'); console.log(parsedJSON.graph_data);

var parsedJSON = $ .parseJSON('{“graph_data”:“{}”}');的console.log(parsedJSON.graph_data);