0

I have a json that start with an object that contains all the rest :

{
 "mainObject": 
 {
    "other stuff here 1" : "aaa",
    "other stuff here 2" : "bbb",
    ...
    "other stuff here 2000" : "zassbbb"
 }
}

I get the entire json and put it into object, how i can discard the "mainObject" part and get only the most inner part? I would to have in the my object :

    "other stuff here 1" : "aaa",
    "other stuff here 2" : "bbb",
    ...
    "other stuff here 2000" : "zassbbb"
Max
  • 87
  • 4
  • 12
  • 1
    Please, show what have you tried so far. Are you using `Json.NET` or `System.Text.Json`? Basically, you can easily do it using `JObject` – Pavel Anikhouski Feb 24 '20 at 20:50
  • I'm currently handling it as strings I remove the parts that don't interest me from the beginning to the end of the string, but the code is very cumbersome – Max Feb 24 '20 at 21:09

1 Answers1

0

You need to map the data to a class which represents the json object. You could paste the json into http://json2csharp.com/ for a simple example.

You will then need to deserialise the json string to the created class.

var obj = JsonConvert.DeserializeObject<YourClass>(yourJsonString);

To then access the data within mainObject:

var contents = obj.MainObject;
var example = contents.OtherStuffHere1;

The contents variable will now only have the data that is in main object and the example variable will contain the string “aaa” as per your example.

You may need to add the using statement and install the Newtonsoft nuget package depending on your dotnet version using Newtonsoft.Json.Linq;

There are lots of examples and resources on the newtonsoft website.

https://www.newtonsoft.com/json/help/html/DeserializeObject.htm

taylorj94
  • 71
  • 3