I have to parse an deserialize all the values from the following JSON file:
But I’m getting errors from the compiler.
[
{
"CompanyInfo": {
"Company": "AdventureWorks",
"Id": "AW01"
},
"ReportsToGenerate":
[
{
"Type_Report": "Type A Report",
"Report_Title": "Human Resources Report",
"Report_Data":
[
{
"BusinessEntityID":168,
"FirstName":"Garrett",
"MiddleName":"R",
"Rate":9.5000
},
{
"BusinessEntityID":278,
"FirstName":"Garrett",
"MiddleName":"R",
"Rate":23.0769
}
]
},
{
"Type_Report": "Type B Report",
"Report_Title": "Sales Report",
"Report_Data":
[
{
"BusinessEntityID":282,
"FirstName":"José",
"MiddleName":"Edvaldo",
"DueDate":"2011-06-12T00:00:00"
},
{
"BusinessEntityID":282,
"FirstName":"José",
"MiddleName":"Edvaldo",
"DueDate":"2011-06-12T00:00:00"
}
]
},
]
}
]
I’ve been trying with this code, But I cannot access the Report_Data
node values:
public class Report
{
public string Type_Report { get; set; }
public string Report_Title { get; set; }
public List<Report_Data> Report_Data { get; set; }
}
public class Report
{
public string Type_Report { get; set; }
public string Report_Title { get; set; }
public List<Report_Data> Report_Data { get; set; }
}
public class Report_Data
{
public List<TypeAStructure> ReportData_TypeAStructure { get; set; }
public List<TypeBStructure> ReportData_TypeBStructure { get; set; }
public List<TypeCStructure> ReportData_TypeCStructure { get; set; }
}
public class TypeAStructure
{
public int BusinessEntityID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string JobTitle { get; set; }
public string OrganizationLevel { get; set; }
public string BirthDate { get; set; }
public string Gender { get; set; }
public string MaritalStatus { get; set; }
public int SickLeaveHours { get; set; }
public string HireDate { get; set; }
public string DepartmentName { get; set; }
public string GroupName { get; set; }
public string ShiftName { get; set; }
public double Rate { get; set; }
}
try
{
jsonContent = await streamReader.ReadToEndAsync();
List<Root> root = JsonConvert.DeserializeObject<List<Root>>(jsonContent);
string sequenceValue = new string(nameFile.Where(char.IsDigit).ToArray());
foreach (var rootItem in root)
{
var companyName = rootItem.CompanyInfo.Company;
var companyId = rootItem.CompanyInfo.Id;
foreach (var report in rootItem.ReportsToGenerate)
{
await Console.Out.WriteLineAsync( "hey");
foreach (var listA in report.Report_Data)
{
await Console.Out.WriteLineAsync( "Hey2");
}
}
}
}
catch (System.Text.Json.JsonException ex)
{
log.LogError(ex, $"There was a deserialization issue with {nameFile}");
}
catch (Exception ex)
{
log.LogError(ex, $"There was a general exception issue with {nameFile}");
}
I was trying with different llops and structures but I cannot access to the information, I am receiving a System Null Reference Exception and I cannot continue with my application.
Each time that I need to access to the content I receive the same error and I really need to get the values from this Node according with the type of report.
I am working With Newtonsoft Library in my Solution.
How can I deserialize and get the values from the ReportData Node in C#, without changing the source file structure?
2
Answers
I’m not 100% percent sure what you are needed to do in your code, but I have needed to do something similar myself. I will share the code I use to parse a List of JSON objects I get from a queue. Hopefully this helps.
I’m using Newtonsoft.Json.Linq to do this.
Here a sample JSON object:
Below is the code that parses each object:
Refer to the below working code: