C# 解析具有多个结果的 JSON 字符串

问题描述 投票:0回答:1

我正在尝试解析具有多个结果的 JSON 字符串。我正在从我们的服务台票务系统调用 API。我能够从 API 中获取所有我想要的票务信息到 var 中,但我无法将其解析为单独的字符串。

这就是我目前正在使用 JSON Newtonsoft 所做的事情。

var ticketData = JsonConvert.DeserializeObject <TicketData> (tickets);

tickets
是 JSON 字符串。它包含正确的数据,但是当我尝试输出其中一个值时

Console.WriteLine(ticketData.summary.ToString());

它不起作用并显示 0 或空值。我可以让它在另一个 JSON 结果中工作,但它只有一个结果。

以下是我的类,定义了此 JSON 中存储的数据:

public class TicketData
{
    public List<Data> datas { get; set; }
}

public class Data
{
    public string assignedAppUser { get; set; }
    public decimal createTime { get; set; }
    public string description { get; set; }
    public int id { get; set; }
    public string priority { get; set; }
    public string requestor { get; set; }
    public string severity { get; set; }
    public decimal solvedTime { get; set; }
    public string source { get; set; }
    public List<Status> statuses { get; set; }
    public string summary { get; set; }
    public string tags { get; set; }
    public string ticketForm { get; set; }
    public string triggeredCondition { get; set; }
    public string type { get; set; }
}

public class Status
{
    public int statusID { get; set; }
    public string displayName { get; set; }
    public int parentID { get; set; }
}

以下是 API 返回的 JSON 示例:

{
   "data":[
      {
         "assignedAppUser":"Nathan",
         "createTime":1674593050.296000000,
         "description":"Hi HD,\n\nI think the Cert Emails need a check up. I don't think that all of them are being generated and going out.",
         "id":1006,
         "priority":"MEDIUM",
         "requester":"Lisa",
         "severity":"MODERATE",
         "solvedTime":1675722696.475000000,
         "source":"EMAIL",
         "status":{
            "statusId":6000,
            "displayName":"Closed",
            "parentId":6000
         },
         "summary":"Lisa Holden / Troubleshoot Missing Cert Renewal Emails",
         "tags":[
            "Programming",
            "Application"
         ],
         "ticketForm":"Default",
         "triggeredCondition":null,
         "type":"PROBLEM"
      },
      {
         "assignedAppUser":"Nathan",
         "createTime":1674593291.600000000,
         "description":"Identify best solution for monitoring battery backup UPS devices in all areas so we can be notified anytime they switch to battery power even if endpoint devices don't go down.",
         "id":1007,
         "priority":"NONE",
         "requester":"Nathan",
         "severity":"NONE",
         "solvedTime":null,
         "source":"TECHNICIAN",
         "status":{
            "statusId":6000,
            "displayName":"Closed",
            "parentId":6000
         },
         "summary":"Configure monitoring on battery UPSs",
         "tags":[
            "Hardware"
         ],
         "ticketForm":"Default",
         "triggeredCondition":null,
         "type":null
      }
   ],
   "metadata":{
      "columns":[
         "createTime",
         "requester",
         "status",
         "type",
         "summary",
         "assignedAppUser",
         "source",
         "id",
         "solvedTime"
      ],
      "sortBy":[
         {
            "field":"id",
            "direction":"ASC"
         }
      ],
      "attributes":{
         
      },
      "filters":null,
      "lastCursorId":2,
      "allColumns":[
         "createTime",
         "requester",
         "status",
         "type",
         "summary",
         "assignedAppUser",
         "source",
         "id",
         "solvedTime"
      ],
      "columnNamesForExporting":[
         "createTime",
         "requester",
         "status",
         "type",
         "summary",
         "assignedAppUser",
         "source",
         "id",
         "solvedTime"
      ],
      "allRequiredColumns":[
         "createTime",
         "requester",
         "status",
         "type",
         "summary",
         "assignedAppUser",
         "source",
         "id",
         "solvedTime",
         "ticketForm",
         "description",
         "priority",
         "severity",
         "tags",
         "triggeredCondition"
      ]
   }
}

如有任何帮助,我们将不胜感激。谢谢。

c# json
1个回答
0
投票

您正在处理的 JSON 结构有一个名为“data”的根元素,其中包含票证信息数组。您的 TicketData 类通过数据对象列表反映了此结构。因此,您应该相应地反序列化它。

您的反序列化代码应如下所示:

var ticketData = JsonConvert.DeserializeObject<TicketData>(tickets);
    
    foreach (var ticket in ticketData.datas)
    {
        Console.WriteLine(ticket.summary);
        // Access other properties as needed
    }

请注意,datas 是 TicketData 类中的属性名称,对应于 JSON 中的“data”数组。迭代此列表以访问每个票证的属性。

© www.soinside.com 2019 - 2024. All rights reserved.