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

  • Home
  • SEARCH
  • 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 9260245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:50:37+00:00 2026-06-18T12:50:37+00:00

So I am attempting to turn my complex object .NET into a totally different

  • 0

So I am attempting to turn my complex object .NET into a totally different object in JSON. Basically, I have an array of objects that could be any type that derives off my base class and I want to turn that array of objects into my own JSON object. I don’t think I can just do a simple JsonConvert.Serialize() method call because my JSON object will be structured differently.

So here is a mockup of my .NET classes:

public abstract class A
{
   public string Id { get; set; }
   public string Title { get; set; }
   public bool Enabled { get; set; }
   public List<X> MyCollection { get; set; }
}

public class B : A
{
   public string Foo { get; set; }
}

public class C : A
{
   public int Bar { get; set; }
}

public abstract class X
{
   public string Id { get; set; }
   public string Title { get; set; }
   public bool Enabled { get; set; }
}

public class Y : X
{
   public string Hello { get; set; }
}

public class Z : X
{
   public string World { get; set; }
}

Obviously that is a simple view of my real class structure but hopefully some guidance on how to convert this will lend me to be able to convert my real class. Basically, my classes (A,B,C) will contain a list of classes (X,Y,C).

So let’s say I have an array/collection of these objects I listed above:

List<A> myObjects = new List<A>();
A myVar = new B();
myVar.Title = "Number 1";
myVar.MyCollection.Add(new Y());
myVar.MyCollection.Add(new Y());
myVar.MyCollection.Add(new Z());
myObjects.Add(myVar);

myVar = new B();
myVar.Title = "Number 2";
myVar.MyCollection.Add(new Z());
myObjects.Add(myVar);

myVar = new C();
myVar.Title = "Number 3";
myVar.MyCollection.Add(new Y());
myVar.MyCollection.Add(new Y());
myObjects.Add(myVar);

I want to take my object ‘myObjects’ and serialize it into a JSON structure like this:

[
   {
      name: "Number 1", //This is the value of A.Title
      type: "B", //This is a NEW property I want to add to the JSON
      enabled: true, //This is the value of A.Enabled
      foo: "SomeValue", //This is the value of B.Foo
      myCollection: [
         { name: "Y1", type: "Y", enabled: true, hello: "SomeValue" }
         { name: "Y2", type: "Y", enabled: true, hello: "SomeOtherValue" }
         { name: "Z1", type: "Z", enabled: true, world: "SomeValue" }
      ]
   },
   {
      name: "Number 2",
      type: "B",
      enabled: true,
      foo: "SomeValue",
      myCollection: [
         { name: "Z2", type: "Z", enabled: true, world: "SomeValue" }
      ]
   },
   {
      name: "Number 3",
      type: "C",
      enabled: true,
      bar: "SomeValue",
      myCollection: [
         { name: "Y3", type: "Y", enabled: true, hello: "SomeValue" }
         { name: "Y4", type: "Y", enabled: true, hello: "SomeOtherValue" }
      ]
   }
]

Basically, I want to add my own properties and structure it a little different in JSON than my .NET object reflects. There are some other properties I want to add to my objects as well but they are not listed here (would may this post EVEN bigger). I basically just need a way to take my objects and serialize them in my own custom way. I need to make sure I serialize the derived type so I can carry along some of those properties though. Can anyone help guide me in the right direction on how to solve this problem?

  • 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-18T12:50:38+00:00Added an answer on June 18, 2026 at 12:50 pm

    You could use the Json.Linq classes to dynamically deserialize them by reading the types after you deserialize the primary class.

    var objects = JArray.Parse(jsonString)
    List<A> objectList = new List<A>();
    foreach (var obj in objects) {
        A newObj = null;
        switch ((string)obj["type"]) {
            case "B":
                newObj = obj.ToObject<B>();
                break;
            case "C":
                newObj = obj.ToObject<C>();
                break;
        }
        newObj.MyCollection.Clear();
        foreach (var x in obj["myCollection"]) {
            var newX = null;
            switch ((string)x["type"]) {
                case "Y":
                    newX = x.ToObject<Y>();
                    break;
                case "Z":
                    newX = x.ToObject<Z>();
                    break;
            }
            newObj.MyCollection.Add(newX);
        }
        objectList.Add(newObj);
    }
    

    As far as I can tell that would handle your Json you posted, and if there’s any errors, sorry I did this completely from memory on a tablet 😛

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am attempting to turn a json string into objects with gson. I have
I am attempting to take a list of objects, and turn that list into
I've got a module that I'm attempting to turn into a proper OTP application.
I am attempting to turn an array list of strings into an arraylist of
I am attempting to send a JSON representation of an object in an email
EDIT: I am attempting to encapsulate a 3rd party com object in .net classes
I am attempting to turn an iPhone app into a Universal App with a
I am attempting to create a Python script that in turn runs the shell
Summary We have an ASP.NET application that allows users to query a SQL Server
I am attempting to parse a JSON feed from Yahoo Pipes to turn it

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.