使用newtonsoft将json字符串反序列化为c#对象

时间:2016-04-15 11:25:28

标签: c# json c#-4.0 serialization json.net

我一直在使用该项目,我必须进行外部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 deserailization对它进行反序列化。

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);

在序列化之前我们可以做任何配置吗? newtonsoft是否有助于该功能?

谢谢。

3 个答案:

答案 0 :(得分:1)

如果您确定此属性中没有数组,那么您可以考虑使用 JsonConverter

public class FakeArrayToNullConverter<T> : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return false;
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        JToken token = JToken.Load(reader);
        if (token.Type == JTokenType.Array)
        {
            return null;
        }
        return  token.ToObject<T>();

    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}

然后对您的模型进行额外的归因:

    [JsonProperty("customer")]
    [JsonConverter(typeof(FakeArrayToNullConverter<Customer>))]
    public Customer Customers { get; set; }

    [JsonProperty("application_address")]
    [JsonConverter(typeof(FakeArrayToNullConverter<ApplicationAddress>))]
    public ApplicationAddress ApplicationAddressList { get; set; }

当您在此属性的JSON字符串中时,它将是一个数组[],您只需使用null对象对其进行反序列化。

答案 1 :(得分:0)

您无法指示反序列化处理&#34; []&#34;作为一个不同的东西,因为它代表一个数组(你确定你永远不会得到这些数组中的客户和地址??)

所以你可以deserialize to an anonymous type然后将它映射到你的结构。

答案 2 :(得分:0)

这只是一个猜测,但你可以检查一下是否有效:

public class ApplicationAddress
{
    private readonly string[] _array = new string[2];
    public string this[int index]
    {
        get { return _array[index]; }
        set { _array[index] = value; }
    }
    public string addreesLine1
    {
        get { return this[0]; }
        set { this[0] = value; }
    }
    public string addreesLine2
    {
        get { return this[1]; }
        set { this[1] = value; }
    }
}
相关问题