我正在尝试扩展此处给出的 JSON.net 示例 http://james.newtonking.com/projects/json/help/CustomCreationConverter.html我有另一个从基类/接口(interface)派生的子类public class Person{ public string FirstName { get; set; } public string LastName { get; set; }}public class Employee : Person{ public string Department { get; set; } public string JobTitle { get; set; }}public class Artist : Person{ public string Skill { get; set; }}List<Person> people = new List<Person>{ new Employee(), new Employee(), new Artist(),};我如何将 Json 反序列化回 List[ { "Department": "Department1", "JobTitle": "JobTitle1", "FirstName": "FirstName1", "LastName": "LastName1" }, { "Department": "Department2", "JobTitle": "JobTitle2", "FirstName": "FirstName2", "LastName": "LastName2" }, { "Skill": "Painter", "FirstName": "FirstName3", "LastName": "LastName3" }]我不想使用 TypeNameHandling JsonSerializerSettings。我特别在寻找自定义 JsonConverter 实现来处理这个问题。网络上关于此的文档和示例非常稀少。我似乎无法在 JsonConverter 中正确实现重写的 ReadJson() 方法。 最佳答案 使用标准 CustomCreationConverter ,我正在努力研究如何生成正确的类型( Person 或 Employee ),因为为了确定这一点,您需要分析 JSON 并且没有使用内置的方法 the131232我找到了一个关于类型转换的讨论线程,结果提供了答案。这是一个链接: Type converting 。所需要的是子类 Create ,覆盖 JsonConverter 方法并创建一个新的抽象 ReadJson 方法,该方法接受 Create1。 The JObject class provides a means to load a JSON object and provides access to the data within this object.重写的 JObject 方法创建一个 ReadJson 并调用 JObject 方法(由我们的派生转换器类实现),传入 0x2518122313431444然后可以分析这个 Create 实例,通过检查某些字段的存在来确定正确的类型。 示例 string json = "[{ \"Department\": \"Department1\", \"JobTitle\": \"JobTitle1\", \"FirstName\": \"FirstName1\", \"LastName\": \"LastName1\" },{ \"Department\": \"Department2\", \"JobTitle\": \"JobTitle2\", \"FirstName\": \"FirstName2\", \"LastName\": \"LastName2\" }, {\"Skill\": \"Painter\", \"FirstName\": \"FirstName3\", \"LastName\": \"LastName3\" }]";List<Person> persons = JsonConvert.DeserializeObject<List<Person>>(json, new PersonConverter());...public class PersonConverter : JsonCreationConverter<Person>{ protected override Person Create(Type objectType, JObject jObject) { if (FieldExists("Skill", jObject)) { return new Artist(); } else if (FieldExists("Department", jObject)) { return new Employee(); } else { return new Person(); } } private bool FieldExists(string fieldName, JObject jObject) { return jObject[fieldName] != null; }}public abstract class JsonCreationConverter<T> : JsonConverter{ /// <summary> /// Create an instance of objectType, based properties in the JSON object /// </summary> /// <param name="objectType">type of object expected</param> /// <param name="jObject"> /// contents of JSON object that will be deserialized /// </param> /// <returns></returns> protected abstract T Create(Type objectType, JObject jObject); public override bool CanConvert(Type objectType) { return typeof(T).IsAssignableFrom(objectType); } public override bool CanWrite { get { return false; } } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { // Load JObject from stream JObject jObject = JObject.Load(reader); // Create target object based on JObject T target = Create(objectType, jObject); // Populate the object properties serializer.Populate(jObject.CreateReader(), target); return target; }}关于c# - 如何在 JSON.NET 中实现自定义 JsonConverter 以反序列化基类对象列表?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8030538/
10-13 02:07