Reputation: 43
I've having issues with parsing JSON data from a php file through to JQuery.I've had quite a difficult time finding the most appropriate way of doing this and any help would be appreciated. It needs to be in a function
At the moment I've gotten to the point of:
$.ajax({
url: "Scripts/Interactions.php",
type: "POST",
dataType: "json",
success: function(data){
$.each(data, function(i, grab){
alert(grab.AgentFullName);
})
}
})
While this works, one issue is it presents a row 'Undefined' before the first row, no matter what column I suggest.
Here is a sample of my Json Output data. It's mostly just random Ipsum Lorem at this text, as I'm still in early stages of development. I've checked the data through various online json format checkers and it's come back as valid.
Thanks in advance for any help!
Upvotes: 2
Views: 2222
Reputation: 336
[ "results", { "InteractionID":"1", "AgentFullName":"Peter Germein",
referring the beginning of your json, you probably want to put "results":, instead of "results",
Upvotes: 0
Reputation: 83366
The first element in your array is not an object, it's a string. That's why you're getting undefined for the first element when you say grab.AgentFullName
—the string "results" has no such property.
You could change
[
"results",
{
"InteractionID":"1",
"AgentFullName":"Peter Germein",
"InteractTopics":"Behaviour, Attendance, Attitude, Performance, Closing",
"InteractDiscussion":"Cras at nisl lorem, a lacin...",
"InteractAction":"Morbi quis nunc in odio eg...",
"InteractNotes":"Quisque et ante ut nis..."
},
to
[
{
"InteractionID":"1",
"AgentFullName":"Peter Germein",
"InteractTopics":"Behaviour, Attendance, Attitude, Performance, Closing",
"InteractDiscussion":"Cras at nisl lorem, a lacin...",
"InteractAction":"Morbi quis nunc in odio eg...",
"InteractNotes":"Quisque et ante ut nis..."
},
Or, were you trying to do this:
{
"results": [
{
"InteractionID":"1",
"AgentFullName":"Peter Germein",
"InteractTopics":"Behaviour, Attendance, Attitude, Performance, Closing",
"InteractDiscussion":"Cras at nisl lorem, a lacin...",
"InteractAction":"Morbi quis nunc in odio eg...",
"InteractNotes":"Quisque et ante ut nis..."
},
{
"InteractionID":"2",
"AgentFullName":"Peter Germein",
"InteractTopics":"Behaviour, Attendance, Attitude, Performance, Closing",
"InteractDiscussion":"....",
"InteractAction":"Morbi quis nunc in ...",
"InteractNotes":"Quisque et ante ut nisi ..."
},
Which would be parsed like this:
$.each(data.results, function(i, grab){
alert(grab.AgentFullName);
})
Upvotes: 2