RESTful ASP.NET webservice - How can I return JSON-serialized Dictionary<string,object>?
I have a RESTful web service that returns JSON-serialized data. It can successfully serialize a Dictionary<string, string>
object, but I would like each function to be able to return Dictionary<string, object>
.
When I return Dictionary<string, string>
, I get the expected JSON response. But when I try to return Dictionary<string, object>
, I get this response:
ReadResponse() failed: The server did not return a response for this request.
So, ON TO THE CODE! Here is the Dictionary<string, object>
code that fails:
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public Dictionary<string, object> Test(String Token, String Id)
{
Dictionary<string, object> testresults = new Dictionary<string, object>();
testresults.Add("Test1Key", "Test1Value");
Dictionary<string, string> innertestresults = new Dictionary<string, string>();
innertestresults.Add("InnerTest1Key", "InnerTest1Value");
innertestresults.Add("InnerTest2Key", "InnerTest2Value");
testresults.Add("Test2Key", innertestresults);
return testresults;
}
And, just for kicks/reference, here is the Dictionary<string,string>
code that works perfectly:
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public Dictionary<string, string> Test(String Token, String Id)
{
Dictionary<string, string> testresults = new Dictionary<string, string>();
testresults.Add("Test1Key", "Test1Value");
testresults.Add("Test2Key", "Test2Value");
testresults.Add("Test3Key", "Test3Value");
return testresults;
}
If anybody has any ideas of how to get this to work (or any alternative ways of doing this to get the same end result), please do let me know! I'm pretty open on how to do this.
On the topic of u开发者_开发问答sage... the reason I need the mix is so that I can return results like this (where the "Data" part could be ANYTHING... not necessarily something with the keys ID, Type, and MaxUsers):
{"Status":"Success","Data":{"ID":"1234","Type":"Live","MaxUsers":"5"}}
{"Status":"Failure","Error":"ID does not exist"}
Thank you all very much!
I can recreate the error you are experiencing but have had no luck getting the service to serialize the Dictionary<string, object>
object automatically.
One workaround is to serialize the object in code using the JavaScriptSerializer class and return the resulting string like so:
using System.Web.Script.Serialization;
...
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public string Test(String Token, String Id)
{
Dictionary<string, object> testresults = new Dictionary<string, object>();
testresults.Add("Test1Key", "Test1Value");
Dictionary<string, string> innertestresults = new Dictionary<string, string>();
innertestresults.Add("InnerTest1Key", "InnerTest1Value");
innertestresults.Add("InnerTest2Key", "InnerTest2Value");
testresults.Add("Test2Key", innertestresults);
JavaScriptSerializer serializer = new JavaScriptSerializer();
string json = serializer.Serialize(testresults);
return json;
}
Hope this helps.
Edit (based on comments)
Ok, spent a little time researching this and it seems as though you need to explicity declare the types that will be present in the object graph when serialization occurs using the ServiceKnownType attribute.
The ServiceKnownType needs to be added as an attribute of your service and in your case you need to declare the Dictionary<string, string>
type like so:
[ServiceKnownType(typeof(Dictionary<string, string>))]
[ServiceContract(Namespace = "WebApplication1")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service...
I was banging my head on similar issue, When service was getting executed without any exception but ajax call was not executing success method and in each browser, error message was not consistent.
When I was returning Dictionary. My object class was having some properties of type DateTime. I found few posts on google regarding DateTime format in JSON and C#. I changed those properties from DateTime to string and worked.
Regards.
精彩评论