处理从 Web 服务器接收到的 json 的优雅方法?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这取决于您想要拥有多少灵活性,以及您需要多少时间来实施完整的解决方案。
如果时间不受限制 - 我建议坚持使用更灵活的解决方案,同时使用
IResponse
和IResponseConverter
来分离职责和关注点。如果时间有限,我建议坚持使用 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
andIResponseConverter
.If time is limited I would suggest to stick with
IResponseConverter
so you would be able to add support of new data formats easily.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