处理从 Web 服务器接收到的 json 的优雅方法?

发布于 2024-11-27 01:10:06 字数 1047 浏览 0 评论 0原文

C# 中处理从 Web 服务器接收到的 json 的更好方法是什么?

是否可以将 System.Json.JsonValue 对象直接传递给响应处理程序?

new FooWebService().FetchSomethingAsync(12, "bar", json =>
    {
        DoSomething1(ConvertJsonToClass1(json["key1"]));
        DoSomething2(ConvertJsonToClass2(json["key2"]));
    });

或者我需要用一些“Response”接口的 json 实现来包装 JsonValue ?

interface IResponse
{ ... }

class JsonResponse : IResponse
{ ... }

new FooWebService().FetchSomethingAsync(12, "bar", response =>
    {
        DoSomething1(ConvertResponseToClass1(response["key1"]));
        DoSomething2(ConvertResponseToClass2(response["key2"]));
    });

或者在将 json 传递给处理程序之前将其转换为众所周知的对象?

interface IResponseConverter
{ ... }

class JsonConverter : IResponseConverter
{ ... }

var service = new FooWebService() 
{
    ResponseConverter = new JsonConverter()
};
service.FetchSomethingAsync(12, "bar", response =>
    {
        DoSomething1(response.Key1);
        DoSomething2(response.Key2);
    });

What way is better in C# to handle json received from web server?

Is it okay to pass System.Json.JsonValue object directly to response handler?

new FooWebService().FetchSomethingAsync(12, "bar", json =>
    {
        DoSomething1(ConvertJsonToClass1(json["key1"]));
        DoSomething2(ConvertJsonToClass2(json["key2"]));
    });

Or I need wrap JsonValue with json implementation of some “Response” interface?

interface IResponse
{ ... }

class JsonResponse : IResponse
{ ... }

new FooWebService().FetchSomethingAsync(12, "bar", response =>
    {
        DoSomething1(ConvertResponseToClass1(response["key1"]));
        DoSomething2(ConvertResponseToClass2(response["key2"]));
    });

Or convert json into well known objects before passing it to handler?

interface IResponseConverter
{ ... }

class JsonConverter : IResponseConverter
{ ... }

var service = new FooWebService() 
{
    ResponseConverter = new JsonConverter()
};
service.FetchSomethingAsync(12, "bar", response =>
    {
        DoSomething1(response.Key1);
        DoSomething2(response.Key2);
    });

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

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

发布评论

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

评论(2

谜泪 2024-12-04 01:10:07

这取决于您想要拥有多少灵活性,以及​​您需要多少时间来实施完整的解决方案。

如果时间不受限制 - 我建议坚持使用更灵活的解决方案,同时使用 IResponseIResponseConverter 来分离职责和关注点。

如果时间有限,我建议坚持使用 IResponseConverter,这样您就可以轻松添加对新数据格式的支持。

It depends on how much flexibility you want to have and on other hand how many time you have to implement a complete solution.

If time is not limited - I would suggest to stick with more flexible solution with separated responsibilities and concerns using both IResponse and IResponseConverter.

If time is limited I would suggest to stick with IResponseConverter so you would be able to add support of new data formats easily.

金兰素衣 2024-12-04 01:10:07

MVC 有 System.Web.Mvc.JsonResult 可能值得一看。

您是否考虑过使用动态类型?这是一个很好的总结和一种与我使用过的技术非常相似的技术:
http://www.drowningintechnicaldebt.com/ShawnWeisfeld/archive/2010/08/22/using-c-4.0-and-dynamic-to-parse-json.aspx

MVC has System.Web.Mvc.JsonResult which might be worth a look.

Have you consider using a dynamic type? Here's a good summary and a technique very similar to one I've used:
http://www.drowningintechnicaldebt.com/ShawnWeisfeld/archive/2010/08/22/using-c-4.0-and-dynamic-to-parse-json.aspx

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