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":"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...
Gabriele Petrioli
197k35 gold badges276 silver badges331 bronze badges
asked Dec 6, 2010 at 0:07
gringoLoco007
8492 gold badges12 silver badges18 bronze badges
-
1Anything wrong? Posting what errors there are would be helpful...BoltClock– BoltClock2010年12月06日 00:08:47 +00:00Commented Dec 6, 2010 at 0:08
-
you do not need to reparse the json. Data parameter should hold the complete json object.Gabriele Petrioli– Gabriele Petrioli2010年12月06日 00:11:02 +00:00Commented Dec 6, 2010 at 0:11
4 Answers 4
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];
answered Dec 6, 2010 at 0:11
alex
492k205 gold badges891 silver badges992 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
gringoLoco007
yeah I tried that from the start and worked, I was trying to make the call shorter, for example :: lang.policy_number :: lang being the object, thanks :)
Matrym
if you're trying to make it shorter, do langObj = data.lang; then you can do langObj.policy_number
getJSON parses the response for you.
You don't need to call parseJSON at all.
answered Dec 6, 2010 at 0:09
SLaks
891k182 gold badges1.9k silver badges2k bronze badges
Comments
Give this a try:
jQuery.getJSON("lang_file.json", function(data) {
alert(data.lang.welcome);
});
answered Dec 6, 2010 at 0:13
Zevan
10.3k3 gold badges35 silver badges48 bronze badges
Comments
var obj = JSON.parse(text);
This line easy to change the JSON data to javascript object
Mi-Creativity
9,66410 gold badges40 silver badges49 bronze badges
Comments
lang-js