简体   繁体   English

C# - 反序列化具有多个对象的 Json 对象

[英]C# - Deserialize Json object having Multiple Objects

I am receiving this string Json format from API.我从 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\"]}]}]"

在此处输入图片说明

I wanted the ReponseData in a List so i did the below.我想要列表中的 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);

It gives the below error on this line.它在这一行给出了以下错误。

Error converting value "[{"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 type 'TO_JsonLogs'.错误转换值"[{"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”。 Path '', line 1, position 256.路径 '',第 1 行,位置 256。

Any idea how to get this data in list.知道如何在列表中获取这些数据。

I was able to deserialize it in this way我能够以这种方式反序列化它

    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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM