RESTful ASP.NET Web 服务 - 如何返回 JSON 序列化字典

发布于 2024-12-07 09:37:21 字数 2190 浏览 0 评论 0原文

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

当我返回 Dictionary 时,我得到了预期的 JSON 响应。但是当我尝试返回 Dictionary 时,我得到以下响应:

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

那么,就看代码吧! 这是失败的 Dictionary 代码

[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代码完美运行的代码:

[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"}

非常感谢大家!

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 usage... 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

℉服软 2024-12-14 09:37:21

我可以重新创建您遇到的错误,但没有运气让服务自动序列化 Dictionary 对象。

一种解决方法是使用 在代码中序列化对象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;
}

希望这有帮助。

编辑(基于评论)

好的,花了一点时间研究这个,似乎您需要明确声明当使用 ServiceKnownType 属性。

ServiceKnownType 需要添加为您的服务,在您的情况下,您需要声明 Dictionary 类型,如下所示:

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

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...
自由范儿 2024-12-14 09:37:21

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

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

问候。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文