3

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!

0

5 Answers 5

2

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);
  } 
}
Sign up to request clarification or add additional context in comments.

1 Comment

I agreed, bypass jQuery entirely
1

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

Comments

0

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.

1 Comment

I think G is probably an internal jQuery function after minification.
0

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

Comments

0

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.

6 Comments

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.
I should also add that I'm assuming that it isn't a string, i.e., the json string has already been "objectified."
How should changing a variable name fix any problems?
@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.
That's a big "excuse me" then. I'll make it up for You.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.