开发者

How do I convert JSON data to javascript object

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

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

Here is my JSON data from the lang_file.json:

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

Here is my jquery code:

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

Thanks...


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];


getJSON parses the response for you.

You don't need to call parseJSON at all.


Give this a try:

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


var obj = JSON.parse(text);

This line easy to change the JSON data to javascript object

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜