我一直在处理一个项目,需要调用外部RESTful服务来获取一些数据。
我遇到的问题是,我从服务中获取的响应在不同的情况下是不同的。例如。
在某种情况下,我会收到以下响应
{
"id":3000056,
"posted_date":"2016-04-15T07:16:47+00:00",
"current_status":"initialized",
"customer":{
"name" : "George",
"lastName" : "Mike"
},
"application_address":{
"addressLine1" : "Lin1",
"addressLine2" : "Lin2",
}
}
在另一种情况下,我得到了以下响应。
{
"id":3000057,
"posted_date":"2016-04-15T07:16:47+00:00",
"current_status":"initialized",
"customer":[],
"application_address":[]
}
这里的问题是,我有以下模型,并且我正在使用Newtonsoft反序列化对其进行反序列化。
public class Response
{
[JsonProperty("id")]
public int Id { get; set; }
[JsonProperty("posted_date")]
public DateTime PostedDate { get; set; }
[JsonProperty("current_status")]
public string CurrentStatus { get; set; }
[JsonProperty("customer")]
public Customer Customer { get; set; }
[JsonProperty("application_address")]
public ApplicationAddress ApplicationAddress { get; set; }
}
public Class Customer
{
public string name { get; set; }
public string lastName { get; set; }
}
public classs ApplicationAddress
{
public string addreesLine1{ get; set; }
public string addreesLine1{ get; set; }
}
对于第一个响应,它将进行反序列化。但对于第二个响应,由于响应包含
[]
用于Customer
和ApplicationAddrees
对象,因此响应未被反序列化。在反序列化时,它被视为数组,但实际上不是。注意:我正在使用以下代码进行反序列化。 Response response = JsonConvert.DeserializeObject(result);
是否有任何配置可以在序列化之前进行?Newtonsoft是否提供该功能?
谢谢。