.NET反序列化JSON多种类型 [英] .NET Deserializing JSON to multiple types

查看:172
本文介绍了.NET反序列化JSON多种类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  <一href="http://stackoverflow.com/questions/11429546/deserializing-json-into-one-of-several-c-sharp-subclasses">Deserializing JSON成几个C#的一个子类

我只读以下JSON模式访问:

I have read-only access following JSON schema:

{ items: [{ type: "cat", catName: "tom" }, { type: "dog", dogName: "fluffy" }] }

我想所有这些反序列化到各自的类型:

I would like to deserialize each of these to their respective type:

class Cat : Animal {
    string Name { get; set; }
}
class Dog : Animal {
    string Name { get; set; }
}

我在这一点上唯一的想法就是将它们反序列化到动态的对象,或词典&LT;字符串,对象&gt; ,然后从那里建造这些对象。

My only thought at this point is to deserialize them to a dynamic object, or Dictionary<string, object> and then construct these objects from there.

我可能会丢失从一个JSON的框架存在的东西....

I may be missing something from one of the JSON frameworks out there....

你会有什么办法呢? =]

What would your approach be? =]

推荐答案

我认为这是可能的,你需要反序列化JSON的,然后从那里建造的对象。直接反序列化到将不可能的,因为解串器将不知道如何具体构造这些对象

I think it's likely you'll need to deserialize the Json then construct the objects from there. Deserializing directly to Cat or Dog won't be possible as the deserializer won't know how to construct these objects specifically.

修改:从大量举债<一href="http://stackoverflow.com/questions/8241392/deserializing-heterogenous-json-array-into-covariant-list-using-json-net">Deserializing异种JSON数组协名单,其中;&GT;使用JSON.NET

像这样的工作:

interface IAnimal
{
    string Type { get; set; }
}

class Cat : IAnimal
{
    public string CatName { get; set; }
    public string Type { get; set; }
}

class Dog : IAnimal
{
    public string DogName { get; set; }
    public string Type { get; set; }
}

class AnimalJson
{
    public IEnumerable<IAnimal> Items { get; set; }
}

class Animal
{
    public string Type { get; set; }
    public string Name { get; set; }
}

class AnimalItemConverter : Newtonsoft.Json.Converters.CustomCreationConverter<IAnimal>
{
    public override IAnimal Create(Type objectType)
    {
        throw new NotImplementedException();
    }

    public IAnimal Create(Type objectType, JObject jObject)
    {
        var type = (string)jObject.Property("type");

        switch (type)
        {
            case "cat":
                return new Cat();
            case "dog":
                return new Dog();
        }

        throw new ApplicationException(String.Format("The animal type {0} is not supported!", type));
    }

    public override object ReadJson(JsonReader reader, Type objectType, JsonSerializer serializer)
    {
        // Load JObject from stream 
        JObject jObject = JObject.Load(reader);

        // Create target object based on JObject 
        var target = Create(objectType, jObject);

        // Populate the object properties 
        serializer.Populate(jObject.CreateReader(), target);

        return target;
    }
}

string json = "{ items: [{ type: \"cat\", catName: \"tom\" }, { type: \"dog\", dogName: \"fluffy\" }] }";
object obj = JsonConvert.DeserializeObject<AnimalJson>(json, new AnimalItemConverter());

这篇关于.NET反序列化JSON多种类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆