Reputation: 18502
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.
Upvotes: 1
Views: 2721
Reputation: 125528
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.
Upvotes: 2
Reputation: 29267
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.
Upvotes: 2