简体   繁体   English

如何将 JSON 响应对象(具有动态对象)转换为 C# 类或可用的结果对象

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

I am using an API for car HP and PCP details.我正在使用汽车 HP 和 PCP 详细信息的 API。 When API returns the response object it has some dynamic object and objects names depending upon the parameters given during JSON call input parameters.当 API 返回响应对象时,它具有一些动态对象和对象名称,具体取决于 JSON 调用输入参数期间给出的参数。 Please tell me how can I convert / deserialize this response object into C# object.请告诉我如何将此响应对象转换/反序列化为 C# 对象。 I tried JSON convert to C# classes but didn't get required results.我尝试将 JSON 转换为 C# 类,但没有得到所需的结果。 Given below in API response object.下面在 API 响应对象中给出。 I need to parse this result object into C# object so i can use it for displaying required values on front end.我需要将此结果对象解析为 C# 对象,以便我可以使用它在前端显示所需的值。

JSON Response / Result Object JSON 响应/结果对象

{
    "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
    }
}

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; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM