2

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.

http://pastebin.com/KFKtiSD4

Thanks in advance for any help!

2 Answers 2

2

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

})
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, the latter is what I'm going for. However how would I correct this in my php file. It uses json Encode. That's probably not enough information, so you can find the contents of Interactions.php here: [link]pastebin.com/3KjDG0sF
@Poika - I'm not a PHP guy. I would ask a new question on how to serialize your PHP data to get what you're looking for.
0
[
   "results",
   {
      "InteractionID":"1",
      "AgentFullName":"Peter Germein",

referring the beginning of your json, you probably want to put "results":, instead of "results",

3 Comments

Thanks, that's what I'm going for. However how would I correct this in my php file. It uses json_ncode. That's probably not enough information, so you can find the contents of Interactions.php here: [link]pastebin.com/3KjDG0sF
try this, in line 12 $return_arr = array("results");, change array("results") to array(). then at any line between line 33 and 37, put in $temp_arr = array();$temp_arr["results"] = $return_arr;$return_arr = $temp_arr; (the point is to set the json array under an array index or column called results)
Sir, you're a complete legend! Thank you for your time!

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.