0

When i try to get value from json data i getting undefined

$.get( baseURL + 'vacation/show/' + name_surname, function( data ) {
    alert(data); // return [{"id":"1","title":"Testas","start":"2015-03-04","end":"2015-03-04"}]
    alert(data.start); // return undefined (should be here 2015-03-04)
});

I need get only start value from data, how can i fix this?

4
  • you should parse it using JSON.parse(data) Commented Mar 17, 2015 at 11:49
  • replace $.get with $.getJSON Commented Mar 17, 2015 at 11:49
  • Your data is a JSON array! So, you would use data[0].start or data[0]["start"] Commented Mar 17, 2015 at 11:57
  • @DawidC - And no need of JSON.parse(data) if there is dataType: 'json' when using $.ajax method. Commented Mar 1, 2022 at 18:04

5 Answers 5

1

If your data is not an object, you need to make an object out of it to be able to access its values. I am not sure what it is at the moment. Hence, I've put a condition to check the type and then alert accordingly.

$.get( baseURL + 'vacation/show/' + name_surname, function( data ) {
    alert(data);
    // return [{"id":"1","title":"Testas","start":"2015-03-04","end":"2015-03-04"}]
    if (typeof data === "object") {        
        alert (data[0].start);
    } else {
        alert (JSON.parse(data)[0].start);
    }
});

A demo@fiddle along the same lines.

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

Comments

0

Use this :

$.get( url, function( data ) {
    /* do stuff */
}, 'json');

or this :

$.getJSON( url, function( data ) {
        /* do stuff */
});

1 Comment

Yeah, always... I got used to it... Even if the answer is correct. Probably jealous people or something.
0

Before Accessing a Json data from 'data' object, you need to parse it to Json Object.

you can achieve this as below:

var parsedJson=$.parseJSON(data)

Here after you can access the json objects like:

  alert(parsedJson[i].start);

where 'i' is the index on N-th data set in the Json object, it can take any numeric value ranging from 0,1,..N

Hope this Helps!!

Comments

0

you can do like following:

$.ajax({
 type: 'GET',
 url: baseURL + 'vacation/show/' + name_surname,
 dataType: 'json',
 success: function(data) {
  for(var i = 0; i < data.length; i++) {
    console.log(data[i].start);
  }
 },
 error: function(e) {
  console.log(error);
 }
});

Comments

0

Ty to all, it works fine now, just wondering, or i doing good in my next step, if i retrieve from array for loop and then push only dates into array again to get min date, or it will not be a duplication of work?

 $.getJSON( baseURL + 'vacation/show/' + name_surname, function( data ) {
    if(data != '') {
        var dates = [];  
        for(var i = 0; i < data.length; i++) {
            var date = new Date(data[i].start);
            dates.push(date);
        }

        var minDate = new Date(Math.min.apply(null, dates));
        var year = minDate.getFullYear();
        var month = minDate.getMonth();     

        $('#calendar').fullCalendar('gotoDate', year, month);
    }
 });

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.