Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
891 views
in Technique[技术] by (71.8m points)

serialization - Cannot Deserialize to a c# List, returns no data using NewtonSoft

My json data looks as follows.

[
   {
      "nPropertyID":1,
      "sAddress_1":"1 Clean Street",
      "sReportField":"GasSafetyRenew",
      "dtReportDate":"2021-12-14T00:00:00",
      "nDaysToGo":325,
      "cReminder":"Y",
      "cValid":"Y",
      "sStatus":"Normal"
   },
   {
      "nPropertyID":1,
      "sAddress_1":"1 Clean Street",
      "sReportField":"BoilerMaintDueDate",
      "dtReportDate":"2021-12-14T00:00:00",
      "nDaysToGo":325,
      "cReminder":"Y",
      "cValid":"Y",
      "sStatus":"Normal"
   },
   {
      "nPropertyID":1,
      "sAddress_1":"1 Clean Street",
      "sReportField":"TenancyExpiration",
      "dtReportDate":"2022-06-30T00:00:00",
      "nDaysToGo":523,
      "cReminder":"Y",
      "cValid":"Y",
      "sStatus":"Normal"
   },
   {
      "nPropertyID":1,
      "sAddress_1":"1 Clean Street",
      "sReportField":"RentProtExpiry",
      "dtReportDate":"2021-07-20T00:00:00",
      "nDaysToGo":178,
      "cReminder":"Y",
      "cValid":"Y",
      "sStatus":"Normal"
   },
   {
      "nPropertyID":1,
      "sAddress_1":"1 Clean Street",
      "sReportField":"LegalProtectionDate",
      "dtReportDate":"2021-07-01T00:00:00",
      "nDaysToGo":159,
      "cReminder":"Y",
      "cValid":"Y",
      "sStatus":"Normal"
   },
   {
      "nPropertyID":1,
      "sAddress_1":"1 Clean Street",
      "sReportField":"BoilerGuaranteeDate",
      "dtReportDate":"2026-02-04T00:00:00",
      "nDaysToGo":1838,
      "cReminder":"Y",
      "cValid":"Y",
      "sStatus":"Normal"
   },
   {
      "nPropertyID":1,
      "sAddress_1":"1 Clean Street",
      "sReportField":"InsuranceRenewBy",
      "dtReportDate":"2020-08-24T00:00:00",
      "nDaysToGo":-152,
      "cReminder":"Y",
      "cValid":"Y",
      "sStatus":"Overdue"
   },
   {
      "nPropertyID":1,
      "sAddress_1":"1 Clean Street",
      "sReportField":"HMOLicenceExpiry",
      "dtReportDate":"2020-01-01T00:00:00",
      "nDaysToGo":-388,
      "cReminder":"Y",
      "cValid":"Y",
      "sStatus":"Overdue"
   },
   {
      "nPropertyID":1,
      "sAddress_1":"1 Clean Street",
      "sReportField":"Tenancy Expiry",
      "dtReportDate":"2020-07-05T00:00:00",
      "nDaysToGo":-202,
      "cReminder":"Y",
      "cValid":"Y",
      "sStatus":"Overdue"
   },
   {
      "nPropertyID":1,
      "sAddress_1":"1 Clean Street",
      "sReportField":"FireCertRenew",
      "dtReportDate":"2020-01-01T00:00:00",
      "nDaysToGo":-388,
      "cReminder":"Y",
      "cValid":"Y",
      "sStatus":"Overdue"
   },
   {
      "nPropertyID":1,
      "sAddress_1":"1 Clean Street",
      "sReportField":"FireServiceRenew",
      "dtReportDate":"2020-01-01T00:00:00",
      "nDaysToGo":-388,
      "cReminder":"Y",
      "cValid":"Y",
      "sStatus":"Overdue"
   },
   {
      "nPropertyID":1,
      "sAddress_1":"1 Clean Street",
      "sReportField":"EmergencyLightingRenew",
      "dtReportDate":"2020-01-01T00:00:00",
      "nDaysToGo":-388,
      "cReminder":"Y",
      "cValid":"Y",
      "sStatus":"Overdue"
   },
   {
      "nPropertyID":1,
      "sAddress_1":"1 Clean Street",
      "sReportField":"FixedEndDate",
      "dtReportDate":"2024-09-30T00:00:00",
      "nDaysToGo":1346,
      "cReminder":"Y",
      "cValid":"Y",
      "sStatus":"Normal"
   }
]

This I believe to be a standard json array.

My c# class is as follows:

 public class Reminder
    {
      
        public int nPropertyID { get; set; }
        public string sAddress_1 { get; set; }
        public string sReportField { get; set; }
        public DateTime dtReportDate { get; set; }
        public int nDaysToGo { get; set; }
        public string cReminder { get; set; }
        public string cValid { get; set; }
        public string sStatus { get; set; }

    }

First I return the data as a string which is fine. I want to deserialize to my variable 'reminders' which is a List. I have tried to deserialize this with:

JsonSerializerSettings settings = new JsonSerializerSettings();

settings.DateFormatString = "dd-MM-yyyyThh:mm:ss";

settings.Error = HandleDeserializerError;

reminders =  JsonConvert.DeserializeObject<List<Reminder>>(json, settings);
    

I am not getting an error. It just returns null.

question from:https://stackoverflow.com/questions/65868620/cannot-deserialize-to-a-c-sharp-list-returns-no-data-using-newtonsoft

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...