将JSON反序列化为List

时间:2018-04-25 17:44:50

标签: c# json asp.net-mvc serialization

我无法将JSON对象反序列化为List。我的JSON对象如下:

[{
    "returnValue": [{
        "facility": "DRIO",
        "recrd_desc": "DEFAULT",
        "update_time": {
            "$date": 1509128545000
        },
        "control_num": 1,
        "starttime": {
            "$date": 1506830400000
        },
        "endtime": {
            "$date": 4102462799000
        },
        "can_exchange_rate": 2000
    },
    {
        "facility": "DRIO",
        "recrd_desc": "TEMP",
        "update_time": {
            "$date": 1521229607000
        },
        "control_num": 37,
        "starttime": {
            "$date": 1513040240000
        },
        "endtime": {
            "$date": 1544576240000
        },
        "can_exchange_rate": 2112
    },
    {
        "facility": "DRIO",
        "recrd_desc": "TEMP 3",
        "update_time": {
            "$date": 1521229399000
        },
        "control_num": 38,
        "starttime": {
            "$date": 1544576580000
        },
        "endtime": {
            "$date": 1576112580000
        },
        "can_exchange_rate": 2000
    }],
    "ok": 1.0
}]

我的C#类是:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CSCApi.Models
{
    public class FareSchedResponse
    {
        public List<FareSched> ReturnValue { get; set; }
        public float Ok { get; set; }
    }

    public class FareSched
    {
        public string facility { get; set; }
        public string recrd_desc { get; set; }
        public Update_Time update_time { get; set; }
        public int control_num { get; set; }
        public Starttime starttime { get; set; }
        public Endtime endtime { get; set; }
        public int can_exchange_rate { get; set; }
    }

    public class Update_Time
    {
        public long date { get; set; }
    }

    public class Starttime
    {
        public long date { get; set; }
    }

    public class Endtime
    {
        public long date { get; set; }
    }
}

正在发生的问题是Update_Time,StartTime和EndTime返回的值是0而不是实际的long值。我不确定错误在哪里。

感谢任何帮助。 NH

反序列化代码:

using Newtonsoft.Json;

[HttpGet]
[Route("api/FareSchedule/GetFareChart/{ActivePlazaID}/{ControlNumber}")]
public async Task<IHttpActionResult> GetFareChartAsync(string ActivePlazaID, int ControlNumber)
{
    try
    {
        var json = await _dbCalls.GetFareChart(ActivePlazaID, ControlNumber);
        var r = ParseResponse<FareSchedResponse>(json);
        return Ok(r.ReturnValue);
    }
    catch (Exception e)
    {
        return BadRequest(e.Message);
    }
}

public static T ParseResponse<T>(string json)
{
    try
    {
        List<T> r = JsonConvert.DeserializeObject<List<T>>(FormatAsList(json));
        if (r != null)
            return r[0];
    }
    catch (Exception)
    {
    }
    return JsonConvert.DeserializeObject<T>(FormatAsList(json));
}

public static string FormatAsList(string json)
{
    try
    {
        var retVal = json;
        if (!string.IsNullOrWhiteSpace(retVal) && retVal.IndexOf(":[") <= 0)
        {
            retVal = retVal.Replace("[{\"returnValue\":", "[{\"returnValue\":[");
            retVal = retVal.Replace(",\"ok\":", "],\"ok\":");
            return retVal;
        }
    }
    catch (Exception)
    {
        // do nothing
    }
    return json;
}

1 个答案:

答案 0 :(得分:0)

正如@maccetura所提到的,你只需要在模型中使用一个属性。这样,您还可以拥有符合C#命名约定的可读属性名称。如果您使用JSON.NET,则可以选择JsonPropertyDataMember属性。

有很多网站可以帮助您生成基于JSON的C#模型。只需谷歌像 JSON到C#

我尝试的第二个生成器非常智能,可以使用JsonProperty属性。以下是我得到的一些例子:

public partial class Time
{
    [JsonProperty("$date")]
    public long Date { get; set; }
}

public partial class ReturnValue
{
    [JsonProperty("facility")]
    public string Facility { get; set; }

    [JsonProperty("recrd_desc")]
    public string RecrdDesc { get; set; }

    [JsonProperty("update_time")]
    public Time UpdateTime { get; set; }  

    //Other properties
}

我希望你能轻松生成剩下的东西。