如何将JSON数据转换为javascript对象

时间:2022-10-30 14:17:06

Can anyone tell me what I doing wrong please...

有人能告诉我我做错了什么吗?

I am trying to convert JSON data to Javascript Object using jQuery's parseJSON

我正在尝试使用jQuery的parseJSON将JSON数据转换为Javascript对象

Here is my JSON data from the lang_file.json:

这是来自lang_file.json的JSON数据:

{"lang":{  
 "welcome":"Welcome to renewals",  
 "policy_number":"Policy Number",  
 "policy_holder_dob":"Policy Holder Date of Birth"  
 }  
}

Here is my jquery code:

下面是我的jquery代码:

jQuery.getJSON("lang_file.json", function(data) {  
 var json2 = data.lang;  
 var obj = jQuery.parseJSON(json2);  
 alert(obj.welcome);  
});

Jquery version : jquery-1.4.2

Jquery版本:jquery-1.4.2

Thanks...

谢谢……

4 个解决方案

#1


5  

You should be able to access any of that data like so already...

你应该已经能够访问这些数据了……

data.lang.welcome;
data.lang.policy_number;
data.lang.policy_holder_dob;

Or you may find it necessary to do this...

或者你可能觉得有必要这么做……

data.lang['policy' + someVar];

#2


5  

getJSON parses the response for you.

getJSON解析响应。

You don't need to call parseJSON at all.

您根本不需要调用parseJSON。

#3


1  

Give this a try:

给这一个尝试:

jQuery.getJSON("lang_file.json", function(data) {  
  alert(data.lang.welcome);  
});

#4


0  

var obj = JSON.parse(text);

This line easy to change the JSON data to javascript object

这一行很容易将JSON数据更改为javascript对象

#1


5  

You should be able to access any of that data like so already...

你应该已经能够访问这些数据了……

data.lang.welcome;
data.lang.policy_number;
data.lang.policy_holder_dob;

Or you may find it necessary to do this...

或者你可能觉得有必要这么做……

data.lang['policy' + someVar];

#2


5  

getJSON parses the response for you.

getJSON解析响应。

You don't need to call parseJSON at all.

您根本不需要调用parseJSON。

#3


1  

Give this a try:

给这一个尝试:

jQuery.getJSON("lang_file.json", function(data) {  
  alert(data.lang.welcome);  
});

#4


0  

var obj = JSON.parse(text);

This line easy to change the JSON data to javascript object

这一行很容易将JSON数据更改为javascript对象