使用NewtonSoft

时间:2019-03-27 10:18:48

标签: c# .net json json.net deserialization

我正在尝试将json反序列化回该对象。
我的对象中有一个接口列表,反序列化失败。
粘贴下面的代码。

型号

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }

    public List<IAddress> Addresses { get; set; }
}

public abstract class IAddress
{
    public string HomeTown { get; set; }
}

public class Address : IAddress
{
    public string Location { get; set; }
    public string Street { get; set; }
    public string Zip { get; set; }
}

要序列化和反序列化的代码如下:

var personObj = new Person
{
    Name = "John Doe",
    Age = 30,
    Addresses = new List<IAddress>
    {
        new Address
        {
            Location = "location1",
            Street = "Vzk",
            Zip = "686670"
        }
    }
};

var json = JsonConvert.SerializeObject(personObj);
System.Console.WriteLine(json);

/*
{
    "Name": "John Doe",
    "Age": 30,
    "Addresses": [{
        "Location": "location1",
        "Street": "Vzk",
        "Zip": "686670",
        "HomeTown": null
    }]
}
*/

var p2 = JsonConvert.DeserializeObject<Person>(json, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All });

System.Console.WriteLine((p2.Addresses[0] as Address).Zip);

我得到的错误是:

  

未处理的异常:Newtonsoft.Json.JsonSerializationException:无法创建Human.IAddress类型的实例。类型是接口或抽象类,无法实例化。路径“地址[0]。位置”,第1行,位置53。      在Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateNewObject(JsonReader Reader,JsonObjectContract objectContract,JsonProperty containerMember,JsonProperty containerProperty,String id,Boolean&createdFromNonDefaultCreator)中      在Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader阅读器,Type objectType,JsonContract合同,JsonProperty成员,JsonContainerContract containerContract,JsonProperty containerMember,对象existValue)中      在Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal处(JsonReader阅读器,Type objectType,JsonContract合同,JsonProperty成员,JsonContainerContract containerContract,JsonProperty containerMember,对象existValue)      在Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList(IList列表,JsonReader阅读器,JsonArrayContract合同,JsonProperty containerProperty,字符串ID)      在Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader阅读器,类型objectType,JsonContract合同,JsonProperty成员,对象existValue,字符串ID)      在Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal处(JsonReader阅读器,Type objectType,JsonContract合同,JsonProperty成员,JsonContainerContract containerContract,JsonProperty containerMember,对象existValue)      在Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty属性,JsonConverter属性Converter,JsonContainerContract containerContract,JsonProperty containerProperty,JsonReader阅读器,对象目标)      在Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject,JsonReader reader,JsonObjectContract contract,JsonProperty member,String id)中      在Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader阅读器,Type objectType,JsonContract合同,JsonProperty成员,JsonContainerContract containerContract,JsonProperty containerMember,对象existValue)中      在Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal处(JsonReader阅读器,Type objectType,JsonContract合同,JsonProperty成员,JsonContainerContract containerContract,JsonProperty containerMember,对象existValue)      在Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader,Type objectType,Boolean checkAdditionalContent)中      在Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader,Type objectType)      在Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader,Type objectType)      在Newtonsoft.Json.JsonConvert.DeserializeObject(字符串值,类型,JsonSerializerSettings设置)      在Newtonsoft.Json.JsonConvert.DeserializeObject [T](字符串值,JsonSerializerSettings设置)      在d:\ expmts \ csharp \ Person \ Parser \ Program.cs:line 82中的Parser.Program.Main(String [] args)中

有什么方法可以使NewtonSoft理解,对于类型IAddressAddress是要启动的类型。

谢谢。

0 个答案:

没有答案