【问题标题】:Returning variable length json list of numeric properties from ASP.NET Web API从 ASP.NET Web API 返回可变长度 json 数字属性列表
【发布时间】:2014-11-09 17:50:14
【问题描述】:

ASP.NET MVC4 Web API 控制器应返回包含数字属性名称和值的 json 结果,如

{ "openTimes":{"1":"09:00","2":"09:15","3":"09:30", ... }}

属性名称以 1 开头。属性和属性值的数量是在代码中创建的。 如何返回这样的结果?

我试过控制器

[HttpPost]
public class TestController : ApiController
 {
    public HttpResponseMessage Post()
    {
   return Request.CreateResponse(HttpStatusCode.OK, new {
      openTimes = new { @"1"="09:00", @"2"="09:15", @"3"="09:30" }
       });
    }
 }

编译错误

Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.

列表中的元素数量也是在运行时确定的。它不像此示例中那样固定。 如何在代码中生成属性名称“1”、“2”及其值的可变长度列表。

如何从 Web API 返回这样的列表?

【问题讨论】:

    标签: c# asp.net-mvc json asp.net-mvc-4 asp.net-web-api


    【解决方案1】:

    你是对的,你不能用数字创建属性,但你绝对可以用属性来装饰属性,并有一个类(output)来包含所有属性。然后使用该类(output)发送响应。

    完整代码:

    创建一个如下所示的类。

    class output
    {
        [Newtonsoft.Json.JsonProperty("1")]
        public string prop1 { get; set; }
        [Newtonsoft.Json.JsonProperty("2")]
        public string prop2 { get; set; }
        [Newtonsoft.Json.JsonProperty("3")]
        public string prop3 { get; set; }
    }
    

    并使用以下发送数据。

    public HttpResponseMessage Post()
    {
        return Request.CreateResponse(HttpStatusCode.OK, new
        {
            openTimes = new output() { prop1 = "09:00", prop2 = "09:15", prop3 = "09:30" }
        });
    }
    

    输出 - {"openTimes":{"1":"09:00","2":"09:15","3":"09:30"}}

    编辑 - 检查 OP 的评论后:- If we have n number of properties like (1,2,3..), then howoutputclass going to handle that?

    解决方案 - 然后直接使用字典

    Dictionary<int, string> dic = new Dictionary<int, string>();
    dic.Add(1, "09:00");
    dic.Add(2, "09:15");
    dic.Add(3, "09:30");
    return Request.CreateResponse(HttpStatusCode.OK, new
    {
        openTimes = dic
        //openTimes = new output() { prop1 = "09:00", prop2 = "09:15"}
    });
    

    【讨论】:

    • 谢谢。响应可以包含 1.. 50 个这样的属性。在您的答案中,属性的数量是硬编码的。如何返回可变数量的属性:有时"1":"09:00" 有时"1":"10:00","2"="10:30",有时"1":"10:00","2"="10:30", "3":"11:00" 等。结果中的属性数量在运行时在代码中确定
    【解决方案2】:

    我认为 Dictionary&lt;string, string&gt; 完全按照您的需要进行序列化。所以你可能可以创建一个类:

    public class TestController : ApiController
    {
        public YourOutput Get()
        {
            var openTimes = new Dictionary<string, string>
            {
                {"1", "0:09"},
                {"2", "2:09"},
                {"3", "1:09"},
            };
    
            return new YourOutput() { openTimes = openTimes };
        }
    }
    
    public class YourOutput
    {
        public Dictionary<string, string> openTimes { get; set; }
    }
    

    当然,另一种选择是直接手动创建您的 json。

    【讨论】:

      【解决方案3】:

      另一种方式:

      public HttpResponseMessage Post()
      {
               HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
               response.Content =
                   new StringContent("{\"openTimes\":{\"1\":\"09:00\", \"2\":\"09:15\", \"3\":\"09:30\"}}");
               return response;
      }
      

      在你的 JS 中:

      var result = JSON.parse(data);
      if (result.openTimes[1] == "09:00"){
         // ...
      }
      

      【讨论】:

        猜你喜欢
        • 2014-04-25
        • 2013-09-21
        • 2014-04-29
        • 2017-06-08
        • 1970-01-01
        • 1970-01-01
        • 2014-01-04
        • 1970-01-01
        • 2016-06-24
        相关资源
        最近更新 更多