-3

At a Rest-API I got an incoming object from Type System.Object.

public async Task<IActionResult> UploadFileRegistration([FromBody]object incomingObject)

This object I would like to parse into my type.

public class MyType
{
  public string name;
  public double age;
}

The incoming object can has one more attribute "version" and the content of version can look different each time.

{"name":"Gisela", "age":29.64}
{"name":"Gisela", "age":29.64, "version":"new"}
{"name":"Gisela", "age":29.64, "version":2.0}
{"name":"Gisela", "age":29.64, "version":["param1":17,"param2":"oho"]}
{"name":"Gisela", "age":29.64, "version":true}

In case this object has a child with name "version". I want to remove this child and put it into a string. In the parent object the child should be removed.

How to do this?

I think of some thing what do's that (made-up code)..

private object o;
private string myVersion; //...
if(o.HasChild("version"))
{
  myVersion = o.Child("version");
  o.DeleteChild("version");
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Frank Mehlhop
  • 1,480
  • 4
  • 25
  • 48

1 Answers1

1

Instead of a system.object, I would try using a Newtonsoft.Json.Linq.JObject

I'm not sure if you need to declare your parameter as dynamic or if JObject will work correctly.

Here is an article that describes it. (It is likely out-dated, but somewhere to start.)

Using JSON.NET for dynamic JSON parsing

n8mob
  • 266
  • 2
  • 9