Retrieving values from Json file
I have Json file with below information.
successalert({
"School_name": "Convent",
"Class":"12th"
});
Here "successalert" returning function name.I am calling this file from jquery.This is running but I want to fetch data "convent" and "12th" in my JavaScript.
when i am writing code like
function successalert(data){
for(var n in data)
alert(n.method+"");
}
This is giving "und开发者_运维技巧efined" result in alert box. Thanks
Use the parseJSON method: http://api.jquery.com/jQuery.parseJSON/
In your current for loop, the n
variable is a string representing the keys "School_name" and "Class". You are trying to access a non-existent property named method
on that string. That's why you're getting undefined
.
You can access the values you're looking for using the following example.
function successalert(data){
for(var n in data) {
alert(data[n]);
}
}
function successalert(data){ for(var n in data) alert(data[n]); }
n is the property, data[n] is the value
精彩评论