.net 如何在C#中从ReportData节点中初始化和获取值?

5w9g7ksd  于 2023-11-20  发布在  .NET
关注(0)|答案(1)|浏览(104)

我必须解析并转换以下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节点中解析和获取值?

lb3vh1jj

lb3vh1jj1#

我不能100%确定你需要在你的代码中做什么,但我自己也需要做一些类似的事情。我将分享我用来解析从队列中获得的JSON对象列表的代码。希望这能有所帮助。
我使用Newtonsoft.Json.Linq来实现。
下面是一个JSON对象示例:

{
  "$id": "1",
  "MessageType": 1,
  "Answers": [
    {
      "$id": "2",
      "Id": 0,
      "Answer": "2",
      "QuestionId": 1,
      "SectionId": 1
    },
    {
      "$id": "3",
      "Id": 0,
      "Answer": "2",
      "QuestionId": 2,
      "SectionId": 1,
    }    
  ],
  "Totals": [
    {
      "$id": "22",
      "Id": 0,
      "SectionId": 1,
      "Total": 9
    },
    {
      "$id": "23",
      "Id": 0,
      "SectionId": 2,
      "Total": 17
    }
  ]
}

字符串
下面是解析每个对象的代码:

foreach (var message in evnt.Records)
{
    var stringMessageBody = message.Body;
    JObject jsonMessageBody = JObject.Parse(stringMessageBody);

    try
    {
        var print = ((JArray)jsonMessageBody["Answers"]).ToString();
        if (print != "[]")
        {
            var messageType = (JValue)jsonMessageBody["MessageType"];
            type = (string)messageType;

            switch ((int)messageType)
            {
                case (int)MessageType.Standard:
                    // Pull the Answers out of the new JObject
                    var answers = (JArray)jsonMessageBody["Answers"];

                    // Send the answers to be processed
                    await ProcessAnswersAsync(answers);
                    break;
                case (int)MessageType.Cat1:
                    // Pull the Answers out of the new JObject
                    var cat1Answers = (JArray)jsonMessageBody["Answers"];
                    var totals = (JArray)jsonMessageBody["Totals"];

                    // Send the answers to be processed
                    await ProcessCat1AnswersAsync(cat1Answers, totals);
                    break;
            }
            Console.WriteLine($"Message {counter} has been processed");
        }
        else
        {
            Console.WriteLine($"Message {counter} was empty, processing next message...");
        }

    }
    catch (Exception ex)
    {
        // Error handling
    }
    Console.WriteLine($"There are {totalMessages - counter} messages left to process...");
    counter ++;
}
Console.WriteLine("All records have been proccessed.");

相关问题