Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.NET将JSON反序列化为多种类型_.net_Json_Deserialization - Fatal编程技术网

.NET将JSON反序列化为多种类型

.NET将JSON反序列化为多种类型,.net,json,deserialization,.net,Json,Deserialization,可能重复: 我具有以下JSON模式的只读访问权限: { items: [{ type: "cat", catName: "tom" }, { type: "dog", dogName: "fluffy" }] } 我想将其中的每一项反序列化为各自的类型: class Cat : Animal { string Name { get; set; } } class Dog : Animal { string Name { get; set; } } 此时,我唯一的想法是将它们

可能重复:

我具有以下JSON模式的只读访问权限:

{ items: [{ type: "cat", catName: "tom" }, { type: "dog", dogName: "fluffy" }] }
我想将其中的每一项反序列化为各自的类型:

class Cat : Animal {
    string Name { get; set; }
}
class Dog : Animal {
    string Name { get; set; }
}
此时,我唯一的想法是将它们反序列化为
动态
对象,或
字典
,然后从那里构建这些对象

我可能遗漏了某个JSON框架中的某些内容


您的方法是什么?=]

我认为您可能需要反序列化Json,然后从那里构造对象。直接反序列化到
Cat
Dog
是不可能的,因为反序列化程序不知道如何具体构造这些对象

编辑:从

类似这样的方法会奏效:

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, object existingValue, 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());
接口IAnimal
{
字符串类型{get;set;}
}
类别:IAnimal
{
公共字符串CatName{get;set;}
公共字符串类型{get;set;}
}
班犬:IAnimal
{
公共字符串DogName{get;set;}
公共字符串类型{get;set;}
}
类AnimalJson
{
公共IEnumerable项{get;set;}
}
类动物
{
公共字符串类型{get;set;}
公共字符串名称{get;set;}
}
类AnimalItemConverter:Newtonsoft.Json.Converters.CustomCreationConverter
{
公共重写IAnimal创建(类型objectType)
{
抛出新的NotImplementedException();
}
公共IAnimal创建(类型objectType,JObject JObject)
{
变量类型=(字符串)jObject.Property(“类型”);
开关(类型)
{
案例“cat”:
返回新的Cat();
案例“狗”:
归还新狗();
}
抛出新的ApplicationException(String.Format(“不支持动物类型{0}!”,type));
}
公共重写对象ReadJson(JsonReader阅读器,类型objectType,对象existingValue,JsonSerializer序列化程序)
{
//从流中加载作业对象
JObject=JObject.Load(读卡器);
//基于JObject创建目标对象
var target=Create(objectType,jObject);
//填充对象属性
填充(jObject.CreateReader(),目标);
回报目标;
}
}
字符串json=“{items:[{type:\'cat\'、catName:\'tom\'}、{type:\'dog\'、dogName:\'fluffy\'}];
object obj=JsonConvert.DeserializeObject(json,new AnimalItemConverter());

谢谢。我同意。我喜欢这个例子,但不幸的是,所有的属性都不是共享的。我已经更新了我的例子来反映这一点。谢谢你的回答。我把我的问题标为重复的,但我认为这是一个更好的答案。回答得好。感谢您的编辑。这里接受的答案可以说比链接问题的答案好得多。