views:

982

answers:

3

I'm trying to solve the problem of passing a 2-dimensional table into JavaScript AJAX application through SOAP web services. I'm trying to pass data into JavaScript web page through ASP.NET web service declared with following attributes:

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]

I need a complex type to be passed into the JavaScript:

[Serializable]
public class PayRateSummary
{
    public string[] EmployeeId;
    public Dictionary<string, string> EmployeeName;
    public string[] PaycodeId;
    public Dictionary<string, Dictionary<string, double?>> EmployeePaycodeRate;
}

[WebMethod(EnableSession = true)]
public DataElements.PayRateSummary EnumPayRates(Guid companyId)
{
}

And web service declared in a pretty standard way:

    <asp:ScriptManager runat="server" ID="ScriptManager1">
        <Services><asp:ServiceReference Path="~/WebService.asmx" /></Services>
    </asp:ScriptManager>

... function RefreshPayRates() { WebService.EnumPayRates(CompanyCurrent, OnPayRatesLoaded, OnFailure); }

For some reason, Dictionary[string,string] is getting passed allright, but not the Dictionary[string,Dictionary[string,string]]:

--> http://vvcap.net/db/8rveoL-FMP6EUikCaqiz.htp

I remember beating my head against the wall in the past to understand, what could be done to pass such objects and never found any solution.

+2  A: 

Have you tried using this: http://code.google.com/p/aspjson/

I'm not familiar with ASP, but in PHP i use something similar to convert PHP native Objects into JSON objects.

Luca Matteis
++ giving javascript what it wants the way it wants it natively ...
le dorfier
+1  A: 

Building on sktrdie's answer, You could use ASP.NET AJAX JSON Web Services to pass and return the data as JSON objects. Here are some examples-

There is a lot of good information on JSON in this question - What is JSON and why would I use it?

EDIT -

At the moment, you say that you are using SOAP, which is an XML-based transport protocol. There is a very succinct article here - Extending an existing ASP.NET Web Service to support JSON - that explains how to extend an existing web service,

In addition, if you do not have access to .NET 3.5 JSON serializer/deserializer, have a look at James Newton King's JSON.NET library.

EDIT 2 -

Looking at your screenshot again and the fact that you have the [ScriptService] attribute, you appear to be returning a JSON string (which I understand is the default). Therefore, I'm thinking is the ASP.NET AJAX JSON serializer failing to serialize the nullable double in this line

public Dictionary<string, Dictionary<string, double?>> EmployeePaycodeRate;

to the correct JSON data type in each instance? You could see whether this is the case by changing the web service to return a double, and then try it with data that have values.

According to MSDN-

Nullable types are also supported and map to JSON in the same way as non-nullable types.

Nonetheless, I think it's worth looking at in a similar manner to as I have suggested, as this is where your problem is occurring

Hope this helps

EDIT 3 -

It seems strange why a Dictionary with a Dictionary value would not be possible to represent in a JSON string. Consider that a Dictionary looks like this in JSON

[{"Key":"a1","Value":"a1"},
{"Key":"a2","Value":"a2"}]

A Dictionary with a Dictionary value would therefore look like so (I believe)

[{"Key":"a1","Value": {"Key":"b1","Value":"b1"} },
{"Key":"a2","Value": {"Key":"b2","Value":"b2"} }]

which is valid JSON according to JSONLint - the JSON validator.

Russ Cam
Russ, isn't that exactly what I am using?..
galets
According to your description, your're using SOAP, which is an XML-based transfer protocol. I'll update my answer with more info
Russ Cam
I'm pretty sure it's a dictionary of dictionary which is the problem. I tried to refactor structure to: public double?[][] EmployeePaycodeRate;and that worked. Not that it's unacceptable, but I would rather prefer dictionary of dictionary, but if this won't work, oh well
galets
A: 

A Dictionary with a Dictionary value would therefore look like so (I believe)

[{"Key":"a1","Value": {"Key":"b1","Value":"b1"} },
{"Key":"a2","Value": {"Key":"b2","Value":"b2"} }]

I absolutely agree with that statement, and this was exactly the goal I've been trying to achieve. I wonder, could there be another way to generate such a JSON?.. Caveat in my case is that the field names are dynamic.

I believe the next C# standard may address this problem with dynamic datatypes

galets