0

I a php array I converted to json and echoed that I want to retrieve using jquery. The echoed json is in the format:

[{"id":"1","title":"Test Event 1","description":"This is the first test event","location":"Acme Hall, Room 101","contact":"John Smith","url":"http:\/\/www.example.com","start":"2016-02-29 13:00:00","end":"2016-02-29 14:00:00"},
{"id":"2","title":"Test Event 2","description":"This is the second test event","location":"Venable Hall, Room 101","contact":"Jane Smith","url":"http:\/\/www.example.com","start":"2016-03-08 09:00:00","end":"2016-03-10 10:45:00"},
{"id":"3","title":"Test Event 3","description":"This is the third test event","location":"Sitterson Hall, Room 200","contact":"Jane Smith","url":"http:\/\/www.example.com","start":"2016-03-18 15:00:00","end":"2016-02-22 16:30:00"}]

When I attempt to use Ajax GET to parse and display the JSON, 'undefined' is returned. My ajax code is:

$.ajax({
    type: 'GET',
    url: 'get-events.php',
    dataType: "json",
    cache: false,
    success: function (result) {
        alert(result[start]);
    },
});

UPDATE:

Thanks for the responses. I finally got it sorted using

for (var key in result) {
 if (result.hasOwnProperty(key)) {
     result[key].id

   }
  }
1
  • What is the variable start? Commented Feb 29, 2016 at 14:12

2 Answers 2

1

Your result is a table, so you have to specify the index: result[index]

Then, json is a {"key" : "value"} format, where key is a string. In your code you used start which is an undefined variable.

So you should try:

alert(result[0]["start"]);
Sign up to request clarification or add additional context in comments.

Comments

0

It seems you don't use the result value the correct way. Result is an array of objects. If you want to access the start value of the first element, you have to use result[0].start.

To access all values, you should use a for-loop to loop over the array.

for (var i = 0; i < result.lenght; i++) {
    // result[i].start
}

Comments

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.