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

Waqas Nadeem 1 Reputation point
2022-06-23T10:45:01.653+00:00

Hi, 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  
    }  
}  
Developer technologies ASP.NET Other
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2022-06-23T13:35:55.527+00:00

    you probably want to use Dictionary<> for the parameter levels. but there is not enough sample data for us to know the rules.

    0 comments No comments

  2. Luis Orostizaga 1 Reputation point
    2022-06-23T20:07:38.647+00:00

    Hi,
    You can check this.

    Check the class: System.Web.Helpers

    dynamic data = Json.Decode(json);
    So serialize a json to a dynamic object

    or check the newtonsoft Json tool
    https://www.newtonsoft.com/json/help/html/QueryJsonDynamic.htm

    Regards,

    0 comments No comments

  3. Yijing Sun-MSFT 7,096 Reputation points
    2022-06-24T02:31:39.117+00:00

    Hi @Waqas Nadeem ,
    If you're using .NET 3.5 or later, I suggest you could use DeserializeObject.

    using System.Runtime.Serialization;  
    using System.Runtime.Serialization.Json;  
      
     public static string Serialize<T>(T obj)  
        {  
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());  
            MemoryStream ms = new MemoryStream();  
            serializer.WriteObject(ms, obj);  
            string retVal = Encoding.UTF8.GetString(ms.ToArray());  
            return retVal;  
        }  
          
        public static T Deserialize<T>(string json)  
        {  
            T obj = Activator.CreateInstance<T>();  
            MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json));  
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());  
            obj = (T)serializer.ReadObject(ms);  
            ms.Close();  
            return obj;  
        }  
    

    Best regards,
    Yijing Sun


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.