我必须解析并转换以下JSON文件中的所有值:
但我从编译器得到错误。
[
{
"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"
}
]
},
]
}
]
字符串
我一直在尝试使用此代码,但我无法访问Report_Data
节点值:
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}");
}
我正在尝试使用不同的llops和结构,但我无法访问信息,我收到了一个系统错误引用异常,我无法继续我的应用程序。
每次我需要访问内容时,我都会收到相同的错误,我真的需要根据报告的类型从这个节点获取值。
我在我的解决方案中使用Newtonsoft Library。
如何在不改变源文件结构的情况下,在C#中从ReportData节点中解析和获取值?
1条答案
按热度按时间lb3vh1jj1#
我不能100%确定你需要在你的代码中做什么,但我自己也需要做一些类似的事情。我将分享我用来解析从队列中获得的JSON对象列表的代码。希望这能有所帮助。
我使用Newtonsoft.Json.Linq来实现。
下面是一个JSON对象示例:
字符串
下面是解析每个对象的代码:
型