0

I am trying to take this JSON:

{
      "title":"string",
      "description":"string",
      "date":"2021-04-19T01:05:38.000Z",
      "image":"url",
      "images":[
         "url1",
         "url2"
      ],
      "attributes":{
         "phonebrand":"x",
         "phonecarrier":"y",
         "forsaleby":"z",
         "price":12345,
         "location":"daLocation",
         "type":"OFFERED"
      },
      "url":"url to listing"
   }

And convert it into this C# Object:

public class Listing {
        [JsonProperty("title")]
        public string Title { get; set; }
        [JsonProperty("description")]
        public string Description { get; set; }
        [JsonProperty("date")]
        public DateTime? Date { get; set; }
        [JsonProperty("image")]
        public string Image { get; set; }
        [JsonProperty("images")]
        public string[] Images { get; set; }
        [JsonProperty("url")]
        public string Url { get; set; }
        [JsonProperty("price")]
        public decimal Price { get; set; }
        [JsonProperty("locationId")]
        public int LocationId { get; set; }
        [JsonProperty("categoryId")]
        public int CategoryId { get; set; }
        [JsonProperty("sortByName")]
        public string SortByName { get; set; }
        [JsonProperty("q")]
        public string Q { get; set; }
        [JsonProperty("location")]
        public string Location { get; set; }
        [JsonProperty("type")]
        public string Type { get; set; }
        [JsonProperty("forsaleby")]
        public string ForSaleBy { get; set; }
        [JsonProperty("fulfillment")]
        public string Fulfillment { get; set; }
        [JsonProperty("payment")]
        public string Payment { get; set; }
        [JsonProperty("phonebrand")]
        public string? PhoneBrand { get; set; }
        [JsonProperty("phonecarrier")]
        public string? PhoneCarrier { get; set; }
}

My problem is, I'm trying to deserialize properties like price and phonebrand but those properties are under an object in the JSON. So when I try to deserialize them like this, those properties can't be found and are set as null. How can I deserialize those properties without changing my C# Class to include an Attributes class? I want to do this because I think that it is a cleaner/better design compared the JSON I'm taking in.

DudeManGuy
  • 123
  • 2
  • 12
  • https://stackoverflow.com/questions/33088462/can-i-specify-a-path-in-an-attribute-to-map-a-property-in-my-class-to-a-child-pr ? – Jeremy Lakeman Apr 19 '21 at 02:42

1 Answers1

1

I suggest two approaches that are very explicit and easy to follow for the next developer looking at the code.

Two classes

creating a intermediate dto class that is used for deserialisation and then creating the business logic object from that intermediate object.

var withAttributes = Deserialise<ListingDto>();

var flatObject = new Listing(withAttributes);

One class

You could provide accessors at the top level which dip into the subclasses.

public class Listing
{
  public AttributesDto Attributes {get; set}

  ... 

  public string Url => Attributes.Url; // Maybe '?.Url'
}
tymtam
  • 31,798
  • 8
  • 86
  • 126
  • I'll be going with the second option. I thought there was some sort of way to do it using JsonProperty or similar but that works nicely. Ty! – DudeManGuy Apr 19 '21 at 04:42