【问题标题】:Json object with array for datasource binding带有用于数据源绑定的数组的 Json 对象
【发布时间】:2015-10-08 09:08:27
【问题描述】:

我有来自返回 JSON 对象的服务器 API 的数据。 我想将此数据绑定到数据绑定源,但其中一个数据未显示,因为它是一个 int 数组

这是 JSON 对象

namespace Example
{
    internal class SampleResponse1
    {
        [JsonProperty("id")]
        public int Id { get; set; }

        [JsonProperty("name")]
        public string Name { get; set; }

        [JsonProperty("nameDecorated")]
        public string NameDecorated { get; set; }

        [JsonProperty("externalId")]
        public string ExternalId { get; set; }

        [JsonProperty("openingDate")]
        public int[] OpeningDate { get; set; }

        [JsonProperty("hierarchy")]
        public string Hierarchy { get; set; }
    }
}

问题是服务器使用整数数组返回的“开放日期”,我需要它作为日期时间。

请建议转换此对象,以便我可以拥有正确的类型

提前致谢

【问题讨论】:

  • int数组如何表示日期?
  • int[0] = 年; int[1] = 月; int[2] = 天

标签: .net arrays json c#-4.0


【解决方案1】:
class SampleResponse1
{
    [JsonProperty("openingDate")]
    public int[] Opening{ get; set; }

    public DateTime OpeningDate
    {
        get
        {
            return new DateTime(Opening[0], Opening[1], Opening[2]);
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多