Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8668343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:11:26+00:00 2026-06-12T18:11:26+00:00

Possible Duplicate: Deserializing JSON into one of several C# subclasses I have read-only access

  • 0

Possible Duplicate:
Deserializing JSON into one of several C# subclasses

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

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.

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

What would your approach be? =]

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T18:11:28+00:00Added an answer on June 12, 2026 at 6:11 pm

    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.

    Edit: borrowing heavily from Deserializing heterogenous JSON array into covariant List<> using JSON.NET

    Something like this would work:

    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());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Deserializing JSON into an object with Json.NET Any ideas of how to
Possible Duplicate: Deserializing XML, how do I access attributes? I am deserializing the following
Possible Duplicate: Split A4 PDF page into two A5 and back again I have
Possible Duplicate: Extracting dollar amounts from existing sql data? I have a column in
Possible Duplicate: How do I create a directory and parent directories in one Perl
Possible Duplicate: Matlab gives wrong answer Can anyone explain to me why the following
Possible Duplicate: regex for URL including query string I have a text or message.
Possible Duplicate: thread with multiple parameters How does one thread a sub with two
Possible Duplicate: How can I combine multiple rows into a comma-delimited list in Oracle?
Possible Duplicate: How to call a JavaScript function from PHP? I have a php

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.