views:

351

answers:

1

Hi - Webmethod returns an array of objects - something like this

{"d":
[[{"Amount":100,"Name":"StackOverflow"},
{"Amount":200,"Name":"Badges"},
{"Amount":300,"Name":"Questions"}]]}

On the client-side, when the JSON is referenced using msg.d, I get a

msg.d is undefined 

error. I am using jQuery JavaScript Library v1.4.2

How do I access the elements in the array of objects?


Adding more findings, code and questions:

  1. I don't see __type in the JSON object that is returned. Does that mean that the object sent from the server is not JSON formatted?
  2. When the __type is not a part of the response, I will not be able to use msg.d? (msg.d is undefined)

Some more: 1. I can access the elements from client side using msg[0][0].Amount - How can I specifically JSON format my return object (from the server)


Code Call to the PageMethods

PageMethods.BuildParticipantAsync($get('<%=hdn_AjaxControls.ClientID %>').value, fOnSuccess, fOnFailure);

function onSuccess(msg)
{
alert(msg.d); //This is undefined
}

Web Method
public static object[] BuildParticipantAsync(string lstSAjaxControls)
{
...//do stuff
 return new object[] { ArrayOfObject };
}
A: 

Guys, here's what I found. Because, I am returning back a object[] and not a composite object of a class. I will not be able to reference using msg.d[0].Amount (as I thought I would be able to). I will have to use msg[0][0].Amount - In a way, it does seem to make a lot of sense.

Narmatha Balasundaram