c# - 使用动态属性名称反序列化 Newtonsoft Json 数据 - C#

标签 c# json json.net

我提到了this question ,这与我的问题类似,但无法完全解决问题,因为数据结构不同,而且我无法弄清楚如何将此解决方案应用于下面给出的示例数据:

JSon response

{
    "result": {
        "RITM2572913": {
            "number": "RITM2572913",
            "state": "1",
            "stage": "fulfillment",
            "Sys_ID": "220e89681b31b384e3a0a79b2d4bcbf3",
            "requested_for": "1d1673c4dbda5b0072a85099dc9619b0",
            "Contoso_requested_for": "requested_for:1d1673c4dbda5b0072a85099dc9619b0,var_name_arr:",
            "Contoso_sc_Purposeofthef5request": "Add",
            "Contoso_Sc_Contactinfo": "Contact ",
            "Contoso_sc_Appname": "Application ",
            "Contoso_sc_Description": "Description",
            "Contoso_special_instructions": "special_instructions:",
            "business_justification": "Justification ",
            "Contoso_business_justification": "busess_justification:Justification",
            "Contoso_catalog_item_footer": "owner_info:"
        }
    }
}

我有这样的响应数据,需要对其进行反序列化以适应下面给出的对象模型:

object Model

public class RITMGETRequestResponse
{
    public RITMDetails result { get; set; }

    public class RITMDetails
    {
        public string business_justification { get; set; }
        public string number { get; set; }
        public string requested_for { get; set; }
        public string stage { get; set; }
        public string state { get; set; }
        public string Sys_ID { get; set; }
        public string var_name_arr { get; set; }
        public string Contoso_business_justification { get; set; }
        public string Contoso_catalog_item_footer { get; set; }
        public string Contoso_requested_for { get; set; }
        public string Contoso_sc_Appname { get; set; }
        public string Contoso_Sc_Contactinfo { get; set; }
        public string Contoso_sc_Description { get; set; }
        public string Contoso_sc_Purposeofthef5request { get; set; }
        public string Contoso_special_instructions { get; set; }
    }
}

在这种情况下,RITM 编号是动态的。我需要获取此 JSON 的 Sys_ID 和其他属性。如何反序列化此 JSON 响应以获取这些值?

最佳答案

简单的例子:

使用 JSONProperty 属性来映射动态属性名称的结果值

class Program
{
    static void Main(string[] args)
    {
        var deserialise = JsonConvert.DeserializeObject<RITMRequestResponse>("{\"result\": {\"123\" : { \"number\" : \"123\" }}}");

        Console.WriteLine(deserialise);
        Console.ReadLine();
    }
}

public class RITMRequestResponse
{
    [JsonProperty(PropertyName = "result")]
    public Dictionary<string, RITMDetails> RITMDetails { get; set; }
}

public class RITMDetails
{
    public string Number { get; set; }
}

关于c# - 使用动态属性名称反序列化 Newtonsoft Json 数据 - C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56377765/

相关文章:

c# - 使用ZeroMQ通过Internet进行客户端/服务器通信

c# - 有没有什么方法可以用所有国家名称填充 winforms 中的组合框,并用所选国家/地区的城市填充另一个组合框?

sql - 在 SQL Server 中从 JSON 中删除空元素

java - 将 JSON 与嵌套数组和 json 进行比较(数组顺序无关紧要)

c# - 将对象名称添加到其序列化字符串中

json - 通过 Json.Net 反序列化 F# Map

c# - 类型推断中需要 "unification"的最简单示例

c# - 返回Task的接口(interface)的同步实现

javascript - XMLHttpRequest 仅适用于 IE

c# - 反序列化具有抽象类型属性的对象