【问题标题】:RESTful ASP.NET webservice - How can I return JSON-serialized Dictionary<string,object>?RESTful ASP.NET Web 服务 - 如何返回 JSON-serialize Dictionary<string,object>?
【发布时间】:2011-09-29 16:41:24
【问题描述】:

我有一个返回 JSON 序列化数据的 RESTful Web 服务。它可以成功序列化一个Dictionary&lt;string, string&gt; 对象,但我希望每个函数都能够返回Dictionary&lt;string, object&gt;

当我返回 Dictionary&lt;string, string&gt; 时,我得到了预期的 JSON 响应。但是当我尝试返回Dictionary&lt;string, object&gt; 时,我得到了这样的回复:

ReadResponse() 失败:服务器没有为此返回响应 请求。

那么,看代码吧! 这是失败的Dictionary&lt;string, object&gt; 代码

[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&lt;string,string&gt; 代码

[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


    【解决方案1】:

    我可以重新创建您遇到的错误,但无法让服务自动序列化 Dictionary&lt;string, object&gt; 对象。

    一种解决方法是使用JavaScriptSerializer 类在代码中序列化对象并返回结果字符串,如下所示:

    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;
    }
    

    希望这会有所帮助。

    编辑(基于 cmets)

    好的,花了一点时间研究这个,似乎您需要使用ServiceKnownType 属性显式声明当序列化发生时对象图中将出现的类型。

    ServiceKnownType 需要添加为您的服务的属性,在您的情况下,您需要像这样声明Dictionary&lt;string, string&gt; 类型:

    [ServiceKnownType(typeof(Dictionary<string, string>))]
    [ServiceContract(Namespace = "WebApplication1")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Service...
    

    【讨论】:

    • 对不起,但我实际上尝试过这个方法 - 因为该方法默认序列化,这会将返回值序列化两次(所以它变成一个字符串,然后转义字符放在引号前面)。 :(
    • 太棒了,您在编辑后添加的部分成功了!非常感谢 :) 我会支持你,但我没有足够的声誉,所以必须做一个“最佳答案”。
    【解决方案2】:

    我在类似的问题上猛烈抨击,当服务被执行时没有任何异常,但 ajax 调用没有执行成功方法,并且在每个浏览器中,错误消息不一致。

    当我返回字典时。我的对象类有一些 DateTime 类型的属性。我在 google 上发现了一些关于 JSON 和 C# 中的 DateTime 格式的帖子。我将这些属性从 DateTime 更改为 string 并工作。

    问候。

    【讨论】:

      猜你喜欢
      • 2012-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-13
      • 1970-01-01
      • 2014-10-22
      相关资源
      最近更新 更多