【发布时间】:2014-12-19 03:09:30
【问题描述】:
我有一个 ASP.NET MVC 应用程序。我正在尝试从我的应用程序中的控制器访问外部 Web 服务。目前,我正在使用这样的网络服务:
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(baseAddress);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var response = await client.GetAsync(GetServiceUrl());
dynamic data = System.Web.Helpers.Json.Decode(...)
}
Web 服务的结果可以在 JSON 中具有三种不同的模式。它们看起来像这样;
架构 1
{
"request":"some info",
"value": [
{"id":1, name:"bill" },
{"id":2, name:"john" }
]
}
模式 2
{
"request":"some info",
"value": [
{ "orderId":"A12345", orderDate:"10-12-2014" },
{ "orderId":"B31234", orderDate:"11-01-2014" },
{ "orderId":"C36512", orderDate:"12-03-2014" },
]
}
模式 3
{
"request":"some info",
"value": [
{ "productId":"11-22-33", "name":"ball", "description":"a round thing" },
{ "productId":"3A-12-52", "name":"tire", "description":"keeps you moving" },
{ "productId":"7D-xy-23", "name":"plate", "description":"something to eat off of." },
]
}
如果可能的话,我想避免编写三个单独的类。我真的只想做两件事:1)计算value数组中的对象数量。 2) 遍历value 数组中的对象并通过Razor 打印出一些值。
我可以在不创建 3 个新类的情况下完成这两件事吗?如果有,怎么做?
谢谢!
【问题讨论】:
-
Dictionary<string, Object>就足够了吗? stackoverflow.com/a/3981380
标签: c# asp.net-mvc