Im trying to convert a JObject to an object. I have a WEB-API with ASP.NET framework where I get an Request-Object which has a object-property. This object-property has a completely unkown structure and can have different properties or child-objects (this depends on the sending system). My problem is to parse/convert these JObject´s to a object.
The serialization of an anonymous object is super easy, cause the object definition is clear, but how can I do this the other way around?
I have the following controller:
[HttpPost]
[Route("insert")]
public IHttpActionResult Insert(RequestObject ro)
{
}
This controller receives the following JSON-Body:
{
"SystemName":"S1_K3",
"MetaInformation":{
"Prop1":"X1",
"Prop3": 12,
"X1":{
"User":"XYZ",
"Session":4
}
}
}
This JSON-Body is defined as a class in my code as the following:
public class RequestObject {
public string SystemName { get; set;}
public object MetaInformation { get; set;}
}
As you can see MetaInformation is from type 'object' so it can be filled up with the informations provided inside the json. But when I receive the object inside my controller, the property MetaInformation is an object of type JObject. How can I achieve that my object looks like the following:
object metaInformation = new{
Prop1="X1",
Prop3=12,
X1=new{
User="XYZ",
Session=4
}
};
Please keep in mind, that I does not know the object structure at runtime.
Thanks to everyone who can help!