0

how do i grab the values of a Person[] object?

below is my server side code:

public Person[] GetPersonList()
{
   //impl code....
   return new Person[0];
}

and my client code:

  $("#btn3").click(function (event) {
                $.getJSON(url', { },
                function (data) {
                    alert(data.Name);
                });
            });

i am getting this result in Firebug:

jsonp1290528639946( [{"Active":true,"Description":"Initial Test","Id":"1","Name":"Test2010","EndDate":"\/Date(-62135578800000-0500)\/","StartDate":"\/Date(1280635200000-0400)\/"}] );

Nick Kahn
  • 19,652
  • 91
  • 275
  • 406

1 Answers1

2

You're returning an array, not just an object, so it would need to be:

$("#btn3").click(function (event) {
  $.getJSON('url', { }, function (data) {
     alert(data[0].Name);
  });
});

Or for example looping through them:

$("#btn3").click(function (event) {
  $.getJSON('url', { }, function (data) {
    $.each(data, function(i, person) {
      alert(person.Name);
    });
  });
});
Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
  • Nick, you think the data i am getting is looks kinda strange numbers? – Nick Kahn Nov 23 '10 at 16:39
  • @Chicagoland - can you clarify? – Nick Craver Nov 23 '10 at 16:41
  • if you look at the array i have start/end date `","EndDate":"\/Date(-62135578800000-0500)\/","StartDate":"\/Date(1280635200000-0400)\/"}] ); ` – Nick Kahn Nov 23 '10 at 16:42
  • @Chicagoland - ah, that's the way WCF is serializing dates, check out this question on turning those into JavaScript `Date` objects: http://stackoverflow.com/questions/206384/how-to-format-json-date – Nick Craver Nov 23 '10 at 16:45