array convert to deserialized output

Guhananth S 1 Reputation point
2021-06-06T02:54:52.6+00:00

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

}

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,208 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,288 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. AgaveJoe 26,146 Reputation points
    2021-06-06T10:40:28.973+00:00

    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($"-----------------");
            }
        }
    }
    
    0 comments No comments