【发布时间】:2021-04-01 21:12:44
【问题描述】:
我正在调用一个以 JSON 形式提供响应的 API。我正在尝试反序列化我很困惑的 JSON 对象以创建基于响应的模型。我的回复将如下所示:
代码:
IRestResponse res = client.Execute(request);
//res.content will be ginving me response as follows :
以字符串格式回复:
"[{\"9305\":{\"headerData\":{\"poNum\":\"9305\",\"vendorId\":\"15963\",\"vendorName\":\"Enlow Construction\",\"poValue\":\"20881.00\",\"currency\":\"USD\",\"utilValue\":\"10440.00\",\"dateCreated\":\"1/2/2020 5:44 am\",\"numInvoices\":\"1\",\"requestorName\":\"Sagun, Ernest\",\"requestorEmail\":\"abc.com\"},\"items\":[{\"item\":\"\",\"itemDesc\":\"TQH - Painting - Site Improvement- 50% down required $10440.50\",\"rate\":\"\",\"amount\":\"20881.00\",\"quantity\":\"\"}]},\"9316\":{\"headerData\":{\"poNum\":\"9316\",\"vendorId\":\"14742\",\"vendorName\":\"Optum Health Financial Services\",\"poValue\":\"-723.75\",\"currency\":\"USD\",\"utilValue\":\"-723.75\",\"dateCreated\":\"1/2/2020 6:24 am\",\"numInvoices\":\"1\",\"requestorName\":\"Sagun, Ernest\",\"requestorEmail\":\"Ernesto.Sagun@C3connect.com\"},\"items\":[{\"item\":\"\",\"itemDesc\":\"Employee Benefits\",\"rate\":\"\",\"amount\":\"-723.75\",\"quantity\":\"\"}]},\"9317\":{\"headerData\":{\"poNum\":\"9317\",\"vendorId\":\"9840\",\"vendorName\":\"Global Equipment Company\",\"poValue\":\"1194.34\",\"currency\":\"USD\",\"utilValue\":\"145.50\",\"dateCreated\":\"1/2/2020 6:30 am\",\"numInvoices\":\"1\",\"requestorName\":\"Sagun, Ernest\",\"requestorEmail\":\"Ernesto.Sagun@C3connect.com\"},\"items\":[{\"item\":\"\",\"itemDesc\":\"Site Improvement Project - Budgeted Capex Spend - Thermostat Guards, Wheelchair and Compact Refrigerator\",\"rate\":\"\",\"amount\":\"1194.34\",\"quantity\":\"\"}]},\"9318\":{\"headerData\":{\"poNum\":\"9318\",\"vendorId\":\"9840\",\"vendorName\":\"Global Equipment Company\",\"poValue\":\"658.16\",\"currency\":\"USD\",\"utilValue\":\"539.70\",\"dateCreated\":\"1/2/2020 6:31 am\",\"numInvoices\":\"1\",\"requestorName\":\"Sagun, Ernest\",\"requestorEmail\":\"abc.com\"},\"items\":[{\"item\":\"\",\"itemDesc\":\"Site Improvement Project - Budgeted Capex Spend - Thermostat Guards\",\"rate\":\"\",\"amount\":\"658.16\",\"quantity\":\"\"}]},\"9322\":{\"headerData\":{\"poNum\":\"9322\",\"vendorId\":\"13423\",\"vendorName\":\"Universal Protection Service, LP dba Allied Universal Security Services\",\"poValue\":\"57150.73\",\"currency\":\"USD\",\"utilValue\":\".00\",\"dateCreated\":\"1/2/2020 6:44 am\",\"numInvoices\":\"1\",\"requestorName\":\"Sagun, Ernest\",\"requestorEmail\":\"abc.com\"},\"items\":[{\"item\":\"\",\"itemDesc\":\"US security guard services for Dec 2019\",\"rate\":\"\",\"amount\":\"57150.73\",\"quantity\":\"\"}]}}]"
当我尝试使用在线转换器创建模型时: https://json2csharp.com/json-to-csharp 我有以下课程:
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class HeaderData {
public string poNum { get; set; }
public string vendorId { get; set; }
public string vendorName { get; set; }
public string poValue { get; set; }
public string currency { get; set; }
public string utilValue { get; set; }
public string dateCreated { get; set; }
public string numInvoices { get; set; }
public string requestorName { get; set; }
public string requestorEmail { get; set; }
}
public class Item {
public string item { get; set; }
public string itemDesc { get; set; }
public string rate { get; set; }
public string amount { get; set; }
public string quantity { get; set; }
}
public class 9305 {
public HeaderData headerData { get; set; }
public List<Item> items { get; set; }
}
public class HeaderData2 {
public string poNum { get; set; }
public string vendorId { get; set; }
public string vendorName { get; set; }
public string poValue { get; set; }
public string currency { get; set; }
public string utilValue { get; set; }
public string dateCreated { get; set; }
public string numInvoices { get; set; }
public string requestorName { get; set; }
public string requestorEmail { get; set; }
}
public class Item2 {
public string item { get; set; }
public string itemDesc { get; set; }
public string rate { get; set; }
public string amount { get; set; }
public string quantity { get; set; }
}
public class 9316 {
public HeaderData2 headerData { get; set; }
public List<Item2> items { get; set; }
}
public class HeaderData3 {
public string poNum { get; set; }
public string vendorId { get; set; }
public string vendorName { get; set; }
public string poValue { get; set; }
public string currency { get; set; }
public string utilValue { get; set; }
public string dateCreated { get; set; }
public string numInvoices { get; set; }
public string requestorName { get; set; }
public string requestorEmail { get; set; }
}
public class Item3 {
public string item { get; set; }
public string itemDesc { get; set; }
public string rate { get; set; }
public string amount { get; set; }
public string quantity { get; set; }
}
public class 9317 {
public HeaderData3 headerData { get; set; }
public List<Item3> items { get; set; }
}
public class HeaderData4 {
public string poNum { get; set; }
public string vendorId { get; set; }
public string vendorName { get; set; }
public string poValue { get; set; }
public string currency { get; set; }
public string utilValue { get; set; }
public string dateCreated { get; set; }
public string numInvoices { get; set; }
public string requestorName { get; set; }
public string requestorEmail { get; set; }
}
public class Item4 {
public string item { get; set; }
public string itemDesc { get; set; }
public string rate { get; set; }
public string amount { get; set; }
public string quantity { get; set; }
}
public class 9318 {
public HeaderData4 headerData { get; set; }
public List<Item4> items { get; set; }
}
public class HeaderData5 {
public string poNum { get; set; }
public string vendorId { get; set; }
public string vendorName { get; set; }
public string poValue { get; set; }
public string currency { get; set; }
public string utilValue { get; set; }
public string dateCreated { get; set; }
public string numInvoices { get; set; }
public string requestorName { get; set; }
public string requestorEmail { get; set; }
}
public class Item5 {
public string item { get; set; }
public string itemDesc { get; set; }
public string rate { get; set; }
public string amount { get; set; }
public string quantity { get; set; }
}
public class 9322 {
public HeaderData5 headerData { get; set; }
public List<Item5> items { get; set; }
}
public class MyArray {
public 9305 9305 { get; set; }
public 9316 9316 { get; set; }
public 9317 9317 { get; set; }
public 9318 9318 { get; set; }
public 9322 9322 { get; set; }
}
public class Root {
public List<MyArray> MyArray { get; set; }
}
但是所有类名 9305,9316,9317... 是如何动态的。如何轻松制作模型以使响应脱轨。
【问题讨论】:
标签: c# asp.net-web-api .net-core