0

Hi I have the following classes.

As you can see there is a Body portion and I have created a json based off of this, however my question is:

Suppose we ignore the Body portion of this, and we just focus on Payment, Items, and Receipt...is there a way to programatically on the fly create a json based off of these three classes? Pretend the Body class is non-existent.

What would something like this look like?

public class Payment
        {
            public string Amount { get; set; }
        }

        public class Receipt
        {
            public string Info { get; set; }
            public string Amount { get; set; }
        }

        public class Items
        {
            public string SKU {get; set;}
        }


        public class Body
        {
            public List<Item> Items { get; set; }
            public List<Payment> Payments { get; set; }
            public Receipt Receipt { get; set; }
        }

I have the json.net dll referenced I'm trying to figure out what I can do to get the three classes into a new class if hypothetically the Body class wasn't there and I had to make it on the fly.

Receipt test = new Receipt();

string DataJson = JsonConvert.SerializeObject(test, Newtonsoft.Json.Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });

I can get the json individually like this however I want one json and formatted since the Items is a List as well as Payment, hopefully this makes sense.

RobertC
  • 123
  • 15
  • 1
    Use an anonymous type? [Returning anonymous types with Web API](https://stackoverflow.com/q/10123371/3744182). – dbc Feb 05 '18 at 01:23
  • 2
    "Pretend the Body class is non-existent." That would be a lot easier if it wasn't mentioned in the question at all... – Heretic Monkey Feb 05 '18 at 01:29
  • Sorry I was just trying to give the end result I want to accomplish, only way I could do it was to show the class...I can remove it if it doesn't help understand what I'm trying to accomplish – RobertC Feb 05 '18 at 01:32
  • This post here: https://stackoverflow.com/questions/45326898/serialize-and-deserialize-json-object-from-separate-classes-without-need-to-have Has what i'm looking for – RobertC Feb 05 '18 at 02:57

0 Answers0