question

GuhananthS-8954 avatar image
0 Votes"
GuhananthS-8954 asked AgaveJoe answered

array convert to deserialized output

Hi
I have a json array. I want to deserialize and map to another class its output. have written code like


Json
[
{"PageNo":1,"FormId":98,"FormType":1,
"DtoInformation":
"{
"TimeEntryDetails":
[
{"Loc":"",
"Set":"",
"LunchStart":"",
"LunchEnd":"",
"WrapTime":"",
"Zone":""
},
{"Loc":"",
"Set":"",
"LunchStart":"",
"LunchEnd":"",
"WrapTime":"",
"Zone":""
},
]





]

HtgOutput.cs
public class HtgOutput{

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

public async Task<HtgOutput> ReadData (SortedFile st)
{
HttpResponse resp= new HttpResponse.ReadData(st.IncomingFile);
if(resp.IsSuccessCode)
{
resp = await response.Content.ReadAsStringAsync();
}
var des = JsonConvert.DeserializeObject(st.IncomingFile);

return ds;
}




}

dotnet-aspnet-core-mvcdotnet-aspnet-mvc
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.

1 Answer

AgaveJoe avatar image
0 Votes"
AgaveJoe answered

The JSON you shared above is invalid. The code does not compile and contains a lot of logical errors. Is there anyway you can provide, at the very least, valid JSON?

Below is an example based on you original code. Feel free to modify the code to suite your needs.

JSON

 [
   {
     "PageNo": 1,
     "FormId": 98,
     "FormType": 1,
     "TimeEntryDetails": [
       {
         "Loc": "loc",
         "Set": "set",
         "LunchStart": "12:00",
         "LunchEnd": "1:00",
         "WrapTime": "e",
         "Zone": "r"
       },
       {
         "Loc": "loc1",
         "Set": "set2",
         "LunchStart": "12:00",
         "LunchEnd": "1:00",
         "WrapTime": "rrr",
         "Zone": "kkkk"
       }
     ]
   }
 ]


Model

     public class TimeModel
     {
         public int PageNo { get; set; }
         public int FormId { get; set; }
         public int FormType { get; set; }
         public 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; }
     }

Implementation

 static async Task Main(string[] args)
 {
    
     string fileName = @"C:\Net5Demos\ConsoleAppCS\data.json";
     List<TimeModel> results = JsonConvert.DeserializeObject<List<TimeModel>>(File.ReadAllText(fileName));
    
     foreach(TimeModel tm in results)
     {
         Console.WriteLine(tm.PageNo);
         Console.WriteLine(tm.FormId);
         Console.WriteLine(tm.FormType);
         foreach(Timeentrydetail detail in tm.TimeEntryDetails)
         {
             Console.WriteLine($"\t{detail.Loc}");
             Console.WriteLine($"\t{detail.Set}");
             Console.WriteLine($"\t{detail.LunchStart}");
             Console.WriteLine($"\t{detail.LunchEnd}");
             Console.WriteLine($"\t{detail.WrapTime}");
             Console.WriteLine($"\t{detail.Zone}");
             Console.WriteLine($"-----------------");
         }
     }
 }
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.