0

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!

Robert Wolf
  • 191
  • 1
  • 11
  • 1
    A `JObject` _is_ an object - you just don't know its properties at compile-time. Does this help? https://stackoverflow.com/questions/16097768 – D Stanley Mar 19 '20 at 20:50
  • 1
    if you can serialize it to some well-known structure like xml or json, there are popular library to parse and deserialize them. You could get a dictionary for instance, or have some predefined options. At _worst_ there is `dynamic` type, but that would be avoided if possible (no compile time check) – Pac0 Mar 19 '20 at 20:58
  • If you need static / strong typed objects, you can have `JsonConverter`s. Otherwise, `dynamic` or `JObject` should be good. – weichch Mar 19 '20 at 21:03
  • Is this what you are asking? [How do I use JSON.NET to deserialize into nested/recursive Dictionary and List?](https://stackoverflow.com/q/5546142/10263) – Brian Rogers Mar 19 '20 at 21:11
  • I´ve updated the description, so it should be more understandable now. – Robert Wolf Mar 20 '20 at 06:28
  • *Anonymous* objects can only be defined at compile time. So if you don't know the structure beforehand, there is not a way to create one at run time. That is exactly what [`JObject`](https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Linq_JObject.htm) is for. It gives you a way to handle an unknown JSON object at runtime. – Brian Rogers Mar 20 '20 at 07:23
  • @BrianRogers I´ve no problem with this, but the object is serialized later to be saved in a MongoDB and there it serializes into JValue etc. but there I need the original JSON. – Robert Wolf Mar 20 '20 at 07:28
  • You need the original JSON of what? The whole RequestObject, or just the MetaInformation? If the former, then use `JsonConvert.SerializeObject` to serialize it. If the latter and it is a JObject, then use `MetaInformation.ToString()` to get the JSON. – Brian Rogers Mar 20 '20 at 07:35

1 Answers1

0

I found a solution for my problem, cause every system could send me other informations I created an IEnumerable<KeyValuePair<string,string>> property instead of my object. So every system can send me their meta informations and the type is clear for serializing and deserializing.

Robert Wolf
  • 191
  • 1
  • 11