tags:

views:

39

answers:

3

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 "undefined" result in alert box. Thanks

A: 

Use the parseJSON method: http://api.jquery.com/jQuery.parseJSON/

Tom Smilack
How does this solve the problem?
jessegavin
Maybe I misunderstood the issue? If he has the JSON object then obj.School_name and obj.Class will contain the data he wants.
Tom Smilack
A: 

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]);
  }
}
jessegavin
A: 

hello have you tried this?

function successalert(data){
     for(var n in data)
        alert(data[n]);
}

n is the property, data[n] is the value

jebberwocky