Home > Net >  How to convert JSON response object (having dynamic objects) into C# classes or usable result object
How to convert JSON response object (having dynamic objects) into C# classes or usable result object

Time:06-24

I am using an API for car HP and PCP details. When API returns the response object it has some dynamic object and objects names depending upon the parameters given during JSON call input parameters. Please tell me how can I convert / deserialize this response object into C# object. I tried JSON convert to C# classes but didn't get required results. Given below in API response object. I need to parse this result object into C# object so i can use it for displaying required values on front end.

JSON Response / Result Object

{
    "success": true,
    "data": {
        "hp": {
            "58995": {
                "48": {
                    "40": {
                        "9000": [
                            {
                                "balance": 58955,
                                "first": 1403.62,
                                "regular": 1403.62,
                                "final": 1413.62,
                                "total": 67423.76,
                                "charges": 8428.76,
                                "apr": 6.92,
                                "apr_nofees": 6.91,
                                "term": 48,
                                "flat": 3.57,
                                "fee_front": "0.00",
                                "fee_back": "10.00",
                                "fee_spread": 0,
                                "ref": "1000.00",
                                "ho": 0,
                                "ho_a": 0,
                                "sb": 0,
                                "id": "12",
                                "product_name": "HP/ML No Fees",
                                "excess_mileage": false
                            }
                        ]
                    }
                }
            }
        },
        "pcp": {
            "58995": {
                "48": {
                    "40": {
                        "9000": [
                            {
                                "balance": 58955,
                                "first": 1251.04,
                                "regular": 1251.04,
                                "final": 8386,
                                "total": 68475.92,
                                "charges": 9480.92,
                                "apr": 6.89,
                                "apr_nofees": 6.89,
                                "rv": 8385,
                                "term": 48,
                                "flat": 3.56,
                                "rv_interest": 1084.68,
                                "charges_ex_rv": 8396.24,
                                "fee_front": "0.00",
                                "fee_back": "1.00",
                                "fee_spread": 0,
                                "ref": 0,
                                "ho": 0,
                                "ho_a": 0,
                                "sb": 0,
                                "id": "25",
                                "product_name": "BNP PCP",
                                "excess_mileage": "7p"
                            }
                        ]
                    }
                }
            }
        },
        "count": 2,
        "taken": 443
    }
}

CodePudding user response:

try this

    var jsonParsed = JObject.Parse(json);
    var count = (int)jsonParsed["data"]["count"];
    var taken = (int)jsonParsed["data"]["taken"];

    ((JObject)jsonParsed["data"]).Descendants()
   .OfType<JProperty>()
   .Where(attr => attr.Name == "count" || attr.Name == "taken")
   .ToList()
   .ForEach(attr => attr.Remove());

    Data data = jsonParsed.ToObject<Data>();
    data.count = count;
    data.taken = taken;

classes

public class Data
{
    public bool success { get; set; }
    public Dictionary<string, Dictionary<string, Dictionary<string, Dictionary<string, Dictionary<string, Product[]>>>>> data { get; set; }
    public int count { get; set; }
    public int taken { get; set; }

public class Product
{
    public int balance { get; set; }
    public double first { get; set; }
    public double regular { get; set; }
    public double final { get; set; }
    public double total { get; set; }
    public double charges { get; set; }
    public double apr { get; set; }
    public double apr_nofees { get; set; }
    public int term { get; set; }
    public double flat { get; set; }
    public string fee_front { get; set; }
    public string fee_back { get; set; }
    public int fee_spread { get; set; }
    [JsonProperty("ref")]
    public string refer { get; set; }
    public int ho { get; set; }
    public int ho_a { get; set; }
    public int sb { get; set; }
    public string id { get; set; }
    public string product_name { get; set; }
    public object excess_mileage { get; set; }
}
  • Related