如何解决反序列化没有根对象和1个数组的json ASP.NET MVC
我正在构建使用第三方API的Web应用程序,并且收到下面的json
{
"CompanyID": 14585,"CompanyName": "The Morgan Group Daytona,LLC","BillingAddressLine": "100 S Beach St #200","BillingAddressCity": "Daytona Beach","BillingAddressState": "Fl","BillingAddressPostCode": "32114","BillingCountryCode": "US","BillingAddress": "100 S Beach St #200\r\nDaytona Beach Fl 32114\r\nUNITED STATES","Phone": null,"Fax": null,"website": null,"TaxNumber": null,"Comments": null,"CurrencyCode": "USD","DefaultTradingTermIDFK": 15,"DateCreated": "2020-09-04T18:25:02","DateUpdated": "2020-09-04T18:25:02","Contacts": [
{
"ContactID": 13781,"CompanyIDFK": 14585,"CompanyName": null,"Firstname": "Test","Lastname": "User","Email": "test@test.com","Mobile": "4075551234","PositionTitle": "Test Title","TimeZone": "Eastern Standard Time","DateCreated": "2020-09-07T02:21:10","DateUpdated": "2020-09-07T02:21:10"
}
]
}
其他API调用的所有其他json响应也没有根对象。目的是使用剃刀在视图上显示此信息。最有效的方法是什么?
到目前为止,我已经创建了此类文件
public class Contact {
public int ContactID { get; set; }
public int CompanyIDFK { get; set; }
public object CompanyName { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
public string Email { get; set; }
public object Phone { get; set; }
public string Mobile { get; set; }
public string PositionTitle { get; set; }
public string TimeZone { get; set; }
public DateTime DateCreated { get; set; }
public DateTime DateUpdated { get; set; }
}
public class Root {
public int CompanyID { get; set; }
public string CompanyName { get; set; }
public string BillingAddressLine { get; set; }
public string BillingAddressCity { get; set; }
public string BillingAddressState { get; set; }
public string BillingAddressPostCode { get; set; }
public string BillingCountryCode { get; set; }
public string BillingAddress { get; set; }
public object Phone { get; set; }
public object Fax { get; set; }
public object website { get; set; }
public object TaxNumber { get; set; }
public object Comments { get; set; }
public string CurrencyCode { get; set; }
public int DefaultTradingTermIDFK { get; set; }
public DateTime DateCreated { get; set; }
public DateTime DateUpdated { get; set; }
public List<Contact> Contacts { get; set; }
}
但是现在我仍然试图找出如何反序列化这样的东西?最简单的方法是这样做。我似乎找不到其他符合这种情况的帖子。
解决方法
当您获取JSON时,可以通过转到https://json2csharp.com/并将其转换为类来加快处理速度。例如,该blob返回以下内容:
def create(self,request,format=None):
line = request.query_params.get('id',None)
if advertiser_id :
advertiser_id = request.query_params.get('ad_code',None
line_code = request.query_params.get('id',None)
if advertiser_code :
advertiser_code = request.query_params.get('ad_code',None)
它返回的类有时会遇到一些小问题,例如,由于您的blob具有很多public class Contact {
public int ContactID { get; set; }
public int CompanyIDFK { get; set; }
public object CompanyName { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public string Mobile { get; set; }
public string PositionTitle { get; set; }
public string TimeZone { get; set; }
public DateTime DateCreated { get; set; }
public DateTime DateUpdated { get; set; }
}
public class Root {
public int CompanyID { get; set; }
public string CompanyName { get; set; }
public string BillingAddressLine { get; set; }
public string BillingAddressCity { get; set; }
public string BillingAddressState { get; set; }
public string BillingAddressPostCode { get; set; }
public string BillingCountryCode { get; set; }
public string BillingAddress { get; set; }
public string Phone { get; set; }
public string Fax { get; set; }
public string website { get; set; }
public string TaxNumber { get; set; }
public string Comments { get; set; }
public string CurrencyCode { get; set; }
public int DefaultTradingTermIDFK { get; set; }
public DateTime DateCreated { get; set; }
public DateTime DateUpdated { get; set; }
public List<Contact> Contacts { get; set; }
}
属性,它只是将它们转换为null
。我将它们更改为object
。
然后您只需使用string
进行转换:
Newtonsoft.Json
ETA
您发布了获取此数据的代码,并注意到您正在使用using(var s = File.OpenRead(@"c:\users\andy\desktop\test.json"))
using(var sr = new StreamReader(s))
using(var jtr = new JsonTextReader(sr))
{
var obj = new JsonSerializer().Deserialize<Root>(jtr);
}
。请注意,WebRequest
是旧的,您应该使用WebRequest
。这是通过HttpClient
下载/反序列化的方式:
HttpClient
如果它仍然返回private static readonly HttpClient _httpClient = new HttpClient();
private static async Task<Root> GetStuffFromThereAsync(string token)
{
using(var req = new HttpRequestMessage(HttpMethod.Get,new Uri("https://www.example.com")))
{
req.Headers.Authorization = new AuthenticationHeaderValue("Bearer",token);
using (var resp = await _httpClient.SendAsync(req))
{
resp.EnsureSuccessStatusCode();
using (var s = await resp.Content.ReadAsStreamAsync())
using (var sr = new StreamReader(s))
using (var jtr = new JsonTextReader(sr))
{
return new JsonSerializer().Deserialize<Root>(jtr);
}
}
}
}
,则可能是您的模型不匹配。
您需要在下面提到的类中使用以下行:
info.plist
课程
Root myDeserializedClass = JsonConvert.DeserializeObject<Root>("This is the your JSON string");
这对我有用,您可以检查一下并让我知道是否有任何疑问
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。