Christopher Klein
Christopher Klein

Reputation: 2793

How to pass class name to JSON DeserializeObject at runtime?

Given that I have JSON like this:

[
  {
    "ct_IncludeinSummary": true,
    "ct_allocationid": "12345",
    "allocationclassname": "group1",
    "allocationname": "name1",
    "opendate": "2018-12-30T05:00:00",
    "closeddate": null,
    "yearendvalue": 2863.93,
    "qrgendvalue": 2.06,
    "ct_risk": 2
  },
  {
    "ct_IncludeinSummary": true,
    "ct_allocationassetid": "5678",
    "allocationclassname": "group2",
    "allocationname": "name2",
    "opendate": "2018-12-30T05:00:00",
    "closeddate": null,
    "yearendvalue": 13538223.76,
    "qrgendvalue": 17337143.84,
    "ct_risk": 3
  },
  {
    "ct_IncludeinSummary": true,
    "ct_allocationassetid": "89012",
    "allocationclassname": "group3",
    "allocationname": "name3",
    "opendate": "2019-11-18T05:00:00",
    "closeddate": null,
    "yearendvalue": 0.0,
    "qrgendvalue": 561480.62,
    "ct_risk": 5
  }
]

I can deserialize the results into a defined model and it works just fine.

var summaryConciseData = JsonConvert.DeserializeObject<List<SummaryConciseData>>(jsonresult);

public class Summaryconcisedata
{
    public Summaryconcisedata(
        bool ct_IncludeinSummary,
        string ct_allocationassetid,
        string allocationclassname,
        string allocationname,
        DateTime opendate,
        DateTime? closeddate,
        double? yearendvalue,
        double? qrgendvalue,
        int ct_risk
        )
    {
        this.IncludeinSummary = ct_IncludeinSummary;
        this.allocationid = ct_allocationassetid;
        this.allocationclassname = allocationclassname;
        this.allocationname = allocationname;
        this.opendate = opendate;
        this.closeddate = closeddate;
        this.yearendvalue = yearendvalue;
        this.qrgendvalue = qrgendvalue;
        this.risk = risk;
    }

    public bool IncludeinSummary { get; }
    public string allocationid { get; }
    public string allocationclassname { get; }
    public string allocationname { get; }
    public DateTime opendate { get; }
    public DateTime? closeddate { get; }
    public double? yearendvalue { get; }
    public double? qrgendvalue { get; }
    public int risk { get; }
}

What I am trying to do is to have a large number of classes (Around 30 in all) I can deserialize and load into models

var summaryConciseData = JsonConvert.DeserializeObject(jsonresult, Type.GetType("API.HelperClass.SummaryConciseData"));

I get the following error:

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'API.HelperClass.SummaryConciseData' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

Any idea on how I this can be a List<> ? I've gone through most all of the similar questions to be found but none refer to any data that is in a List

Upvotes: 0

Views: 1284

Answers (1)

Guru Stron
Guru Stron

Reputation: 142048

MakeGenericType on list/ienumerable type should work just fine:

var listType = typeof(List<>).MakeGenericType(Type.GetType("API.HelperClass.SummaryConciseData");
var summaryConciseData = JsonConvert.DeserializeObject(jsonresult, listType);

Upvotes: 1

Related Questions