2

This is a simplified code. I have a webservice (.asmx) as following: I store some values in Test class and then store the class in an arraylist.
I do this two times in two different arraylists. and then store these two array lists in a third arraylist. and then pass this arraylist as output of webmethod.

 private class Test
        {
            public string Id;
            public string Name;
        }

[webmethod]

      public ArrayList RuleReport(long RuleId)
    {
        Test t = new Test();
        t.Id = "1";
        t.Name = "a";

        ArrayList ar = new ArrayList();
        ArrayList ar2 = new ArrayList();
        ArrayList ar3 = new ArrayList();

        ar.Add(t);

t = new Test();
        t.Id = "2";
        t.Name = "b";
        ar2.Add(t);
        ar3.Add(ar);
        ar3.Add(ar2);
        return ar3;
    }

and in js file I want to parse he json result to read each Id and Name values of two arraylists.

id=1,name=a
id=2,name=b

this is my jquery code:

 $.ajax(
{ url: " Ajaxes/Rules.asmx/RuleReport",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    type: "POST",
    data: "{'RuleId':'79'}",
    async: false,
    success: function(data) {
    $.each(data.d, function(index, obj) {
    alert(obj.d[0].Id);// something like this. How to do it???
        })

    }, error: function() { }
});

this is the json response in fire bug:

{"d":[[{"Id":"1","Name":"a"}],[{"Id":"2","Name":"b"}]]}

how to get every Id and Name values???

1 Answer 1

3

With your current setup inside $.each loop you are getting

[{"Id":"1","Name":"a"}]

as obj. As you can see it's a array of object with only one object as it's content. You can access that object with obj[0] and then their properties can be accessed with obj[0].Id and obj[0].Name

You can do this with the following code

$.each(data.d,function(index,obj){

       var id = obj[0].Id;
       var name = obj[0].Name;
       // do what ever you want with them
   })​

Working fiddle

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

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.