C# Parse JSON String with Multiple Results [已关闭]

dzjeubhm  于 12个月前  发布在  C#
关注(0)|答案(3)|浏览(161)

**已关闭。**此问题为not reproducible or was caused by typos。目前不接受回答。

这个问题是由错字或无法再重现的问题引起的。虽然类似的问题在这里可能是on-topic,但这个问题的解决方式不太可能帮助未来的读者。
21小时前关闭
Improve this question
我正在尝试解析一个JSON字符串,其中包含多个结果。我正在从我们的帮助台票务系统调用一个API。我可以从API获取我想要的所有票务信息,但我无法将其解析为单个字符串。
这就是我目前使用JSON Newtonsoft所做的。

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

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

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


它不工作,显示0或null值。我可以让它在另一个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"
      ]
   }
}


任何帮助都将不胜感激。谢谢。

wwtsj6pe

wwtsj6pe1#

您需要创建这样的对象来MapJSON内容

public class Root
{
    public List<Data> data { get; set; }
    public Metadata metadata { get; set; }
}

 public class Metadata
{
    public List<string> columns { get; set; }
    public List<SortBy> sortBy { get; set; }
    public Dictionary<string, object> attributes { get; set; }
    public object filters { get; set; }
    public int? lastCursorId { get; set; }
    public List<string> allColumns { get; set; }
    public List<string> columnNamesForExporting { get; set; }
    public List<string> allRequiredColumns { get; set; }
}

public class Data
{
    public string assignedAppUser { get; set; }
    public double createTime { get; set; }
    public string description { get; set; }
    public int id { get; set; }
    public string priority { get; set; }
    public string requester { get; set; }
    public string severity { get; set; }
    public double? solvedTime { get; set; }
    public string source { get; set; }
    public Status status { get; set; }
    public string summary { get; set; }
    public List<string> tags { get; set; }
    public string ticketForm { get; set; }
    public object triggeredCondition { get; set; }
    public string type { get; set; }
}

public class SortBy
{
    public string field { get; set; }
    public string direction { get; set; }
}

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

字符串
然后“data”是对象的一个元素,所以你可以像这样访问它
你可以像这样得到或从下面的文件中得到

var ticketData = JsonConvert.DeserializeObject<Root>("replace json here");


Expanded results

var ticketData = JsonConvert.DeserializeObject<Root>(File.ReadAllText("replace json file here"));
var firstSummary = ticketData.data[0].summary;
Console.WriteLine(firstSummary);


将您的Json输入替换为此处的文件
您可以从here中了解更多信息

dgiusagp

dgiusagp2#

您的列表位于'data'中,因此在转换之前需要将其与json字符串分离。您可以尝试:

JObject o = JObject.Parse(tickets);
var ticketData = JsonConvert.DeserializeObject <TicketData> (o["data"]);
Console.WriteLine(ticketData.summary.ToString());

字符串

33qvvth1

33qvvth13#

您正在处理的JSON结构有一个名为“data”的根元素,其中包含一个ticket信息数组。您的TicketData类用一个Data对象列表反映了这个结构。因此,您应该相应地将其转换。
您的验证代码应该如下所示:

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”数组。迭代此列表以访问每个票据的属性。

相关问题