我一直在使用该项目,我必须使外部RESTful服务调用来获取某些数据。将json字符串反序列化为使用newtonsoft的c#对象
我在这里面临的问题是,我从服务中得到的回应在不同情况下是不同的。例如。
在一个场景中,我得到如下回应
{
"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":[]
}
这里的问题是,我有以下的模型,而且我通过牛顿软删除来反序列化它。
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
对象,响应包含[]
。反序列化时,它被视为一个数组,但事实上并非如此。
注意:下面的代码我用于反序列化。 响应响应= JsonConvert.DeserializeObject(result);
在序列化之前我们可以做任何配置吗?牛顿软件是否有利于该功能?
谢谢。
能否请您分享您的通话反序列化? –
对于不同的场景,使用不同的类进行反序列化。第一个示例使用您拥有的类,第二个示例使用列表,List 属性的类。您可以为共享属性使用基类。它是您为不同场景调用的单个终端吗? –
@TimBourguignon,我用反序列化代码更新了问题。 – PaRsH