【问题标题】:C# - Deserialize Json object having Multiple ObjectsC# - 反序列化具有多个对象的 Json 对象
【发布时间】:2021-02-11 20:01:44
【问题描述】:

我正在从 API 接收此字符串 Json 格式。

string rawJson = "[{\"RequestID\":12345,\"Status\":100,\"ResponseMessage\": \"API Call Successful\",\"ResponseData\":[{\"EmployeeID\":\"1824\",\"MatchedDateTime\":[\"20 Oct 2020 06:41:45 AM\"]},{\"EmployeeID\":\"1214\",\"MatchedDateTime\":[\"20 Oct 2020 06:05:03 AM\"]}]}]"

我想要列表中的 ReponseData,所以我做了以下操作。

 public class TO_JsonLogs
    {
        [Newtonsoft.Json.JsonProperty("ResponseData")]
        public Dictionary<string, TO_JsonPunches> TO_JsonPunch { get; set; }
             
    }

    public class TO_JsonPunches
    {
        public string EmployeeID { get; set; }
        public string MatchedDateTime { get; set; } 
    }

var logsJson = JsonConvert.DeserializeObject<TO_JsonLogs>(rawJson);

它在这一行给出以下错误。

将值 "[{"RequestID":12345,"Status":100,"ResponseMessage": "API Call Successful","ResponseData":[{"EmployeeID":"1824","MatchedDateTime":["20 Oct 2020 06:41:45 AM"]},{"EmployeeID":"1214","MatchedDateTime":["20 Oct 2020 06:05:03 AM"]}]}]" 转换为类型“TO_JsonLogs”时出错。路径 '',第 1 行,位置 256。

知道如何在列表中获取这些数据。

【问题讨论】:

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


【解决方案1】:

我能够以这种方式反序列化它

    public class Request
    {
        public string RequestID;
        public int Status;
        public string ResponseMessage;

        public List<ResponseDataItem> ResponseData;
    }

    public class ResponseDataItem
    {
        public string EmployeeID;
        public List<string> MatchedDateTime;
    }

    var logsJson = JsonConvert.DeserializeObject<List<Request>>(rawJson);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-15
    • 2016-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多