tags:

views:

180

answers:

5

Hello,

I have some JSON that looks like the following:

[{"Age":35,"FirstName":"Peyton","LastName":"Manning"},  
 {"Age":31,"FirstName":"Drew","LastName":"Brees"},  
 {"Age":58,"FirstName":"Brett","LastName":"Favre"}]

This JSON is passed into a function I have created called parseJSON. This function looks like this:

function parseJSON(result)
{
  $.each(result.items,function(i,item) {
    alert("First Name: " + item.FirstName);
    alert("Age: " + item.Age);
  });
}

When I run this code though, I get a JavaScript error that says "G is undefined". How do I parse my JSON with JQuery such that I can read the FirstName and Age properties?

Thank you!

A: 

You are using a variable called result that doesn't exist in your function. Perhaps, it should be replaced with json.

function parseJSON(json) 
{ 
  $.each(json,function(i,item) { 
    alert("First Name: " + item.FirstName); 
    alert("Age: " + item.Age); 
  }); 
}

EDIT: based on your update, I think it's because you are referencing a property on result that doesn't exist, namely items. The null value being passed into the each function is causing jQuery to choke on an internal function.

tvanfosson
Nope. The object doesn't have an `items` property. It's simply an array of objects. Each object has an `Age`, `FirstName`, etc. property, so I think it is correct as I have it. -- this was in response to a comment that it should be `json.items` instead of simply `json`.
tvanfosson
I should also add that I'm assuming that it isn't a string, i.e., the json string has already been "objectified."
tvanfosson
How should changing a variable name fix any problems?
naugtur
@naugtur -- check the edit history on the question. Originally, the function didn't have result in it. You're downvoting me because my answer reflected the OP's omission.
tvanfosson
That's a big "excuse me" then. I'll make it up for You.
naugtur
I've edited my post here so you can change your vote, if you want.
tvanfosson
This is the correct answer.
mikezter
A: 

Check your complete code, not only the sample shown here for "G". I suppose, there is a syntax error somewhere. As to the use of "result" - it could be a global variable, but tvanfosson is right I think. Doesn't look good here.

cwaidner
I think G is probably an internal jQuery function after minification.
tvanfosson
A: 

Here is a working function that uses your sample case.

var result = [{
    "Age": 35,
    "FirstName": "Peyton",
    "LastName": "Manning"
}, {
    "Age": 31,
    "FirstName": "Drew",
    "LastName": "Brees"
}, {
    "Age": 58,
    "FirstName": "Brett",
    "LastName": "Favre"
}];

function parseJSON(result){
    $.each(result, function(index, object){
        $.each(object, function(i, o){
            alert(i + " = " + o);
        })
    })
}

parseJSON(result);
Singularity
+1  A: 

Most simple parse JSON by native JS:

function parseJSON(result)
{
  for(var i in result) {
    var item = result[i];
    alert("First Name: " + item.FirstName);
    alert("Age: " + item.Age);
  } 
}
I agreed, bypass jQuery entirely
Kristoffer S Hansen
A: 

From json.org: json_sans_eval. This parses a JSON string without the hazards of using eval.

geowa4