c# load Json into inherited class

c# json
c# json deserialize derived class
jsonconverter derived class
type json c#
c# serialize subclass
c# json deserialize child object
newtonsoft json custom deserializer for property
c# newtonsoft json type

I have a C# classes and I need to parse JSON into it.

The class has a List<> from another class.

The class structure is like this.

public class OrderFund {
  public int OrderID { get; set; }
  public int BrokerID { get; set; }
  public string SettlementMethod { get; set; }
  public List<SettlementSap> SettlementsSap { get; set; }
}

public class SettlementSap {
  public string SapMonetaryAccountNo { get; set; }
  public string SapMonetaryAccountType { get; set; }
  public string SapMonetaryAccountOffice { get; set; }
}

My JSON is like this.

{
  "settlementMethod": "SAP",
  "BrokerID": 1,
  "OrderID": 1,
  "Settlements": [
    {
      "SapMonetaryAccountNo": "400245892464",
      "SapMonetaryAccountType": "CA",
      "SapMonetaryAccountOffice": "AR"
    }
  ]
}

I load my JSON file like this...

static OrderFund LoadJson(string file) {
  string dire = Directory.GetCurrentDirectory();
  using (StreamReader r = new StreamReader(dire + "\\" + file)) {
    string json = r.ReadToEnd();
    OrderFund items = JsonConvert.DeserializeObject<OrderFund>(json);
    return items;
  }
}

The data load fine into OrderFun Class but OrderFund.SettlementsSap is null. How can I load Settlements into SettlementsSap?

That's because you have named the field SettlementsSap but your Json field is called Settlements...

You could rename the field in your class;

 public class OrderFund 
    {
        public int OrderID { get; set; }
        public int BrokerID { get; set; }
        public string SettlementMethod { get; set; }
        public List<SettlementSap> Settlements { get; set; }
    }

or add a [JsonProperty("Settlements")] attribute to the field like so;

public class OrderFund 
{
    public int OrderID { get; set; }
    public int BrokerID { get; set; }
    public string SettlementMethod { get; set; }
    [JsonProperty("Settlements")] 
    public List<SettlementSap> SettlementsSap { get; set; }
}

Deserialize JSON into multiple inherited classes, I need to be able to put any type of "Control" in my Control list in DocDB and have C# deserialize that list back into the proper Control class (be  You could try serializing your objects yourself with Json.NET and then posting the serialized content into DocumentDb. Then, when you need the data, read it back as a json string and use Json.NET again to deserialize. Json.NET can handle inheritance, so you just have to configure it to be aware of your type hierarchy. Use the TypeNameHandling

You just use a function of Visual Studio which convert your json into a model class

Goto: Edit -> Paste special -> Paste JSON as Class

The model class created by this feature will solve your problem

So, visiblely, you must rename SettlementsSap by Settlements

        public class OrderFund
        {
            public string settlementMethod { get; set; }
            public int BrokerID { get; set; }
            public int OrderID { get; set; }
            public Settlement[] Settlements { get; set; }
        }

        public class Settlement
        {
            public string SapMonetaryAccountNo { get; set; }
            public string SapMonetaryAccountType { get; set; }
            public string SapMonetaryAccountOffice { get; set; }
        }

How to serialize and deserialize JSON using C#, On the client side we will be using consuming the service from a Portable Class Library PCL that will be able to run with most C# stacks e.g.  So lets have a look at the project setup. The server will be your standard ASP.Net Web API restful web service that comes with JSON.Net out of the box. On the client side we will be using consuming the service from a Portable Class Library PCL that will be able to run with most C# stacks e.g. Windows Store, .Net, Xamarin.iOS, Xamarin.Android etc..

The problem is with the naming. In the JSON, the name is Settlements. But in the class definition of OrderFund it is named as SettlementsSap

Serialize object inheritance with JSON.Net, In my previouos post I've created a very simple, home made class to serialize / deserialize an object without Get it with NuGet: Install-Package Newtonsoft.​Json. If you really think about it why don't serialize in JSON? Serializing and Deserializing inherited types in xml, simple objectsIn ".NET". C# . If you’re using Json.NET to serialize objects which involve inheritance hierarchies, there’s a little issue you might run into. Let’s say we have a class Person: public class Person { public string Name { get; set; } }. …and we also have another class Employee which derives from Person:

Serializing and deserializing inherited types with Json, anything you , I am trying to serialize a list of results into a JSON string. I have a base class for all of my results, and have a few derived classes that add Will I have to parse the results collection and have a switch statement to cast the list  Imagine you need to process some JSON that looks like this: Notice how the array contains similar objects, two vehicles. Each vehicle has a "type" property that identifies the specific type of vehicle. If we wanted to deserialize this into C# we could use the following classes: The challenge is how to tell Json.NET which…

[SOLVED] JSON Serialization of Derived Classes, Inheritance (Derived and Base Class). In C#, it is possible to inherit fields and methods from one class to another. We group the "inheritance concept" into two  JSON To CSharp (C#) Class Converter. JSON To C Sharp Class Converter helps you to convert JSON to c# class online.

C# Inheritance, C#. Copy. public class JObject : JContainer, IDictionary<string, JToken>, ICollection<KeyValuePair<string, JToken>>, IEnumerable<KeyValuePair<string, JToken>>, IEnumerable, Returns a collection of the child tokens of this token, in document order. (Inherited from Asynchronously loads a JObject from a JsonReader. Using the XmlElementAttribute attribute to change the name of an XML element is not the only way to customize object serialization. You can also customize the XML stream by deriving from an existing class and instructing the XmlSerializer instance how to serialize the new class. For example, given a Book class, you can derive from it and create

Comments
  • change public List<SettlementSap> SettlementsSap { get; set; } to public List<SettlementSap> Settlements { get; set; }
  • There is a Miss match between the Json and your class. Json say the properties is name Settlements when The class has a properies named SettlementsSap. You can modify your class to accept this by stackoverflow.com/questions/19792274/…
  • Possible duplicate of Alternate property name while deserializing
  • did you copy the last question? ahaha
  • @MarcoSalerno You means ?
  • I saw a question with the same solution posted by another user which you copypasted like some mins ago, (you answered differently in that question)
  • @MarcoSalerno haha. I answer the question and learn the others better answers at the same time :)
  • yeah np we are all here to get better and help! :P