简体   繁体   中英

Json.NET Deserialize Property Without Deserializing Parent Object Above It

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.

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'
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM