1

I have a question about JSON.NET and deserializing nested JSON.

I was wondering if I could extract some properties out of an API response without making a class to store the parent object using JsonConvert.DeserializeObject.

Example JSON:

{
  "data": [
    {
      "type": "player",
      "id": "account.59daecb0cc3144f7bf6e60c8caabf454",
      "attributes": {
        "createdAt": "2018-04-03T21:11:16Z",
        "name": "UppyMeister",
        "patchVersion": "",
      }
  }]
}

Example Class:

public class Player
{
    [JsonProperty]
    public string Name { get; set; }
    [JsonProperty]
    public DateTime CreatedAt { get; set; }
    [JsonProperty]
    public string PatchVersion { get; set; }
}

So far, I haven't found a way to make this work. Obviously I've tried making a seperate class called "Attributes" that store these properties, and then keeping player as the root object, which of course works, but I was wondering if there's any way to make this work without having to make multiple classes to represents all the objects while still using JsonConvert.DeserializeObject<IEnumerable<Player>>(json);

Thanks

EDIT: Forgot to mention that It's not as easy as just parsing the json string into a json object and then instead deserializing json["data"][0]["attributes"] as I need to store other properties in the class, such as "type" and "id" in this example.

Thomas Upson
  • 239
  • 6
  • 19
  • Have you looked at using the [`JsonExtensionData`](https://www.newtonsoft.com/json/help/html/SerializationAttributes.htm) attribute? It will store data that otherwise doesn't fit elsewhere in the model into a collection. – Ron Beyer Apr 06 '18 at 18:02
  • You could probably use `JsonPathConverter` from [Can I specify a path in an attribute to map a property in my class to a child property in my JSON?](https://stackoverflow.com/a/33094930/3744182) to map nested properties to your `Player` class. Then use `DeserializeAnonymousType()` as shown in [this answer](https://stackoverflow.com/a/4753410/3744182) to deserialize the root data array. – dbc Apr 06 '18 at 18:15

2 Answers2

0

The below should do the trick for you. It uses the ToObject method on JToken.

JObject.Parse(json)["data"][0]["attributes"].ToObject<Player>()

or

JObject.Parse(json).SelectToken("$.data[0].attributes").ToObject<Player>()

TylerBrinkley
  • 1,066
  • 10
  • 16
  • Read what I put in the edit though. This works perfectly if the only data required was those mentioned properties but I need other properties outside the attributes object as well – Thomas Upson Apr 06 '18 at 17:13
-2

You will need to create two classes data and attributes

class data

public class data{
      public string type {get; set;}
      public string id {get; set;}
      public player_attributes attributes {get; set;}
}

public class player_attributes{
      public string createdAt {get; set;}
      public string name {get; set;}
      public string patchVersion {get; ;set;}
}
JaYdipD
  • 40
  • 10
  • I’ve already done this and it works, as mentioned in the post. My question however is whether or not I can get the properties within the attributes object WITHOUT making a separate class for it – Thomas Upson Apr 06 '18 at 17:14