tags:

views:

97

answers:

2

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.

A: 

JSON is really easy to access.

To get the data out of the variable you can do

var value = jsonvar['key']; or

var value = jsonvar.key;

or if you have JSON item that is an array you can do

var arraylist = [];

for (int i=0;i<jsonvar.key.length;i+=1){
    arraylist[i] = jsonvar.key[i];
}

or you can just access the items directly by jsonvar.key[arrayindex].

This tutorial shows you all the different ways to access data in the json element.

AutomatedTester
+1  A: 

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!

TampaRich