0

Put it simply, I am trying this DeserializeObject method: var page = JsonConvert.DeserializeObject<OneColumnPage>(response.Content);

This is currently not working.

Let's say I have this interface:

public interface IPageType
{
    [JsonProperty("@type")]
    string Type { get; set; }

    [JsonProperty("name")]
    string Name { get; set; }

    [JsonProperty("title")]
    string Title { get; set; }

    [JsonProperty("metaKeywords")]
    string MetaKeywords { get; set; }

    [JsonProperty("metaDescription")]
    string MetaDescription { get; set; }

    [JsonProperty("headerContent")]
    IList<IContentType> HeaderContent { get; set; }

    [JsonProperty("mainContent")]
    IList<IContentType> MainContent { get; set; }
}

I have an implementing class which is defined as:

public class OneColumnPage : IPageType
{
    public string Type { get; set; }
    public string Name { get; set; }
    public string Title { get; set; }
    public string MetaKeywords { get; set; }
    public string MetaDescription { get; set; }
    public IList<IContentType> HeaderContent { get; set; }
    public IList<IContentType> MainContent { get; set; }
}

Currently, I am having difficulties deserializing the MainContent. How can I generate the MainContent dynamically? I have several implementations of IContentType.

I have 2 examples of classes implementing the IContentType:

public class RichTextMain : IContentType
{
    public string Type { get; set; }
    public string Name { get; set; }

    [JsonProperty("content")]
    public string Content { get; set; }
}

public class HorizontalRecordSpotlight : IContentType
{
    public string Type { get; set; }
    public string Name { get; set; }

    [JsonProperty("title")]
    public string Title { get; set; }

    [JsonProperty("records")]
    public List<Record> Records { get; set; }
}
  • *Currently, I am having difficulties deserializing the MainContent* what does `MainContent` in your original JSON look like? What does your implementation of `IContentType` look like? What does your code that attempts to deserialize the JSON look like? – Matt Burland May 01 '17 at 19:00
  • Looks like a duplicate of [How to implement custom JsonConverter in JSON.NET to deserialize a List of base class objects?](https://stackoverflow.com/q/8030538/3744182) and [Deserializing polymorphic json classes without type information using json.net](https://stackoverflow.com/q/19307752/3744182). – dbc May 01 '17 at 19:05

0 Answers0