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

本文关键字:对象 反序列化 字符串 newtonsoft json 使用 | 更新日期: 2024-09-20 18:46:09

我一直在处理这个项目,在那里我必须进行外部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":[]
}

这里的问题是,我有下面的模型,我正在通过牛顿软去轨道化对它进行反序列化。

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

对于第一个响应,它将取消序列化。但对于第二个响应,响应没有被反序列化,因为响应包含CustomerApplicationAddrees对象的[]。在反序列化时,它被视为一个数组,但实际上并不是。

注意:下面是我用来反序列化的代码。响应响应=JsonConvert.DescializeObject(结果);

在序列化之前,我们可以做什么配置吗?newtonsoft有助于实现这一功能吗?

谢谢。

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

如果您确信此属性中不会有数组,那么您可以考虑使用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对象对其进行反序列化。

您不能指示反序列化将"[]"处理为不同的东西,因为它代表一个数组(您确定永远不会在这些数组中获得客户和地址吗?)

因此,您可以反序列化为匿名类型,然后将其映射到您的结构。

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

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