question

GuhananthS-8954 avatar image
0 Votes"
GuhananthS-8954 asked Bruce-SqlWork commented

Issue in IList deserialize

Hi
public class TimeModel
{
public int PageNo { get; set; }
public int FormId { get; set; }
public int FormType { get; set; }
public IList<Timeentrydetail>TimeEntryDetails { get; set; }
}

  public class Timeentrydetail
  {
      public string Loc { get; set; }
      public string Set { get; set; }
      public string LunchStart { get; set; }
      public string LunchEnd { get; set; }
      public string WrapTime { get; set; }
      public string Zone { get; set; }
  }

Deserialize Ilist how to do ?

dotnet-aspnet-core-mvc
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

You asked the same question in a previous thread. The code to deserialize a IList<T> and array are the same. Share your code if you are still having an issue as your previous thread contains a lot of C# logical errors.


0 Votes 0 ·
Bruce-SqlWork avatar image
0 Votes"
Bruce-SqlWork answered Bruce-SqlWork commented

iList is an interface, and the Json deserializer does not know what class to use. You can define it in the class, so the deserializer does not have to create it.

 public class TimeModel
 {
     public int PageNo { get; set; }
     public int FormId { get; set; }
      public int FormType { get; set; }
      public IList<Timeentrydetail>TimeEntryDetails { get; set; } = new List<Timeentrydetail>();
 }
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi
public class TimeModel
{
public int PageNo { get; set; }
public int FormId { get; set; }
public int FormType { get; set; }
public IList<Timeentrydetail>TimeEntryDetails { get; set; }
public IList<PayBreakdown>paybreakdown{ get; set; }
public IList<jobinformation>jobinfomation{ get; set; }
}

Deserialize gives only TimeEntryDetails . Paybreakdown,jobinformation count is 0 .
List<TimeModel> dt= "":
dt = JsonConvert.DeserializeObject<TimeModel>(json);

how to do

0 Votes 0 ·

Again. the Json deserializer can not create an IList object. It can add to an IList collection, but can not create one (unless you supply a custom helper). For json poco objects it’s simpler to use an array or List<>

If you use IList in a json poco object, be sure it’s always initialized to the collection type of your choice.

0 Votes 0 ·
ChaoDeng-MSFT avatar image
0 Votes"
ChaoDeng-MSFT answered

Hi @GuhananthS-8954 ,
According to the model you provide, you can do this. First, we can set some parameters and then serialize it into Json format, and then deserialize it, like this.

1.First perform json serialization

  var TimeModel = new TimeModel()
             {
             PageNo=1,
             FormId=1,
             FormType=1,
             TimeEntryDetails=new List<Timeentrydetail>()
             { 
                    new Timeentrydetail()
                    {
                        Loc="Loc",
                        Set="Set",
                        LunchStart="LunchStart",
                        LunchEnd="LunchEnd",
                        WrapTime="WrapTime",
                        Zone="Zone"
                    } ,
             }           
             };
             var options = new JsonSerializerOptions { WriteIndented = true };
             string jsonString = JsonSerializer.Serialize(TimeModel, options);
    
             Console.WriteLine(jsonString);

Result:
W2BGo.png

2.Deserialize json data

   TimeModel timeModel =
                JsonSerializer.Deserialize<TimeModel>(jsonString);
    
             Console.WriteLine($"PageNo: {timeModel.PageNo}");
             foreach (Timeentrydetail item in timeModel.TimeEntryDetails)
             {
                 Console.WriteLine($"Loc:{item.Loc}");
                 Console.WriteLine($"Set:{item.Set}");
                 Console.WriteLine($"LunchStart:{item.LunchStart}");
                 Console.WriteLine($"LunchEnd:{item.LunchEnd}");
                 Console.WriteLine($"WrapTime:{item.WrapTime}");
                 Console.WriteLine($"Zone:{item.Zone}");           
    
             }

Result:
KYI4J.png

I hope these are helpful to you, or you can refer to the official documentation.
How to serialize and deserialize (marshal and unmarshal) JSON in .NET



If the answer is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Best Regards,

ChaoDeng


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.