Search code examples
jquerywcf-client

how to get values of object?


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)\/"}] );


Solution

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