【发布时间】:2011-09-29 16:41:24
【问题描述】:
我有一个返回 JSON 序列化数据的 RESTful Web 服务。它可以成功序列化一个Dictionary<string, string> 对象,但我希望每个函数都能够返回Dictionary<string, object>。
当我返回 Dictionary<string, string> 时,我得到了预期的 JSON 响应。但是当我尝试返回Dictionary<string, object> 时,我得到了这样的回复:
ReadResponse() 失败:服务器没有为此返回响应 请求。
那么,看代码吧! 这是失败的Dictionary<string, object> 代码:
[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;
}
而且,仅供参考,这是完美运行的Dictionary<string,string> 代码:
[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;
}
如果有人对如何使其工作有任何想法(或任何其他方法来获得相同的最终结果),请告诉我!我对如何做到这一点持开放态度。
关于使用的主题...我需要混合的原因是我可以返回这样的结果(其中“数据”部分可以是任何东西...不一定是键 ID、Type 和 MaxUsers):
{"Status":"Success","Data":{"ID":"1234","Type":"Live","MaxUsers":"5"}}
{"Status":"Failure","Error":"ID does not exist"}
非常感谢大家!
【问题讨论】:
标签: asp.net json rest serialization