0

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
    }
}
10
  • In JavaScript and JSON objects are actually dictionaries. You can deserialize JSON parts into Dictionary<T,V> in C# as well. There are several duplicate questions that ask the same thing. The answers show that "objects" with arbitrary keys can be deserialized to Dictionary<string,....> types Commented Jun 23, 2022 at 11:09
  • how to read json as .net objects deserialize - learn.microsoft.com/en-us/dotnet/standard/serialization/… Commented Jun 23, 2022 at 11:10
  • Just how dynamic is this dynamic structure? It looks like at least 4 levels of dictionaries will be needed. Is hp dynamic as well? Is the innermost object well defined? With so many levels it may be better to deserialize to a JObject and try to retrieve items element by element. A Dictionary<string,Dictionary<string,Dictionary<string,Dictionary<string,ThatObject>>>>>>>>>>>>>>>>>>> isn't fun Commented Jun 23, 2022 at 11:12
  • Does this answer your question? stackoverflow.com/a/3806407/1221208 Commented Jun 23, 2022 at 11:12
  • 1
    @PanagiotisKanavos yes that comment was added automatically when I flagged the question.. by the way after retracting my flag I edited my comment to include an answer to SO that did just that (deserialize json dynamic object in c#) with few lines. There are plenty of questions like this on SO Commented Jun 23, 2022 at 11:16

1 Answer 1

2

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; }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot. it works for me.. Stay blessed dear.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.