C# – Need a simple example of a web service call Returning a List of object and how to parse them out in javascript

cjson

I have a simple web service with a web method that creates a list of objects..

 public string GetPersonList()
 {
   Person p1 = new Person { Name = "Rich", Age = "33" };
        Person p2 = new Person { Name = "Rebekah", Age = "34" };
        Person p3 = new Person { Name = "John", Age = "20" };
        List<Person> p = new List<Person>() { p1,p2,p3};

        JavaScriptSerializer oSerializer = new JavaScriptSerializer();
        string jSon = oSerializer.Serialize(p);

        return jSon;
    }

What I want to do is access this on the client side onSuccess callback. here are my javascript functions..

    function GetJson() {
        json.UserService.GetPersonList(DisplayList, YouFailed);
    }

    function DisplayList(e) {

        var vals = '(' + e + ')';

        alert(vals);
    }

    function YouFailed() {
        alert("fail");
    }

Can someone point me to a decent tutorial or provide an explanation on how to accomplish this. I do not know the syntax to access the fields of an array of arrays.

Best Answer

I ended up using a mix between json2 and jqueries $.ajax call. Got the example from Dave Wards blog... http://encosia.com/2009/04/07/using-complex-types-to-make-calling-services-less-complex/

Thank you AutomatedTester for the response though!

Related Topic