【问题标题】:Deserialize nested JSON into Dictionary, string and class将嵌套的 JSON 反序列化为字典、字符串和类
【发布时间】:2018-03-02 07:26:15
【问题描述】:

我有一个 JSON,我想将其反序列化为带有 innerDictionary 和 innermostClass 的外层字典:

var entityMap = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, fieldClass>>>(File.ReadAllText("map.json"));

但是,innerDictionary 可能有一个 string:string 而不是 string:innerMostClass。

{
"Client": {
    "__class__": "contact",

    "ClientId": {
        "__field__": "new_ndisclientid",
        "__type__": "string"
    },
    "GivenName": {
        "__field__": "firstname",
        "__type__": "string"
    },
},
"Case": {
    "__class__": "contact",

    "CaseId": {
        "__field__": "new_ndiscaseid",
        "__type__": "string"
    }
}
}

有没有办法做到这一点?我不想把它全部变成类。

是否可以使用自定义 JsonConverter 来做到这一点?

编辑:为清楚起见,将类名重命名为 entityName。 ClientId 和 GivenName 将被反序列化为 fieldClasses。

【问题讨论】:

    标签: c# json class dictionary nested


    【解决方案1】:

    你可以使用动态或对象来代替内部类

        var json =
            "{\r\n\t\"Client\": {\r\n\t\t\"__entityName__\": \"contact\",\r\n\r\n\t\t\"ClientId\": {\r\n\t\t\t\"__field__\": \"new_ndisclientid\",\r\n\t\t\t\"__type__\": \"string\"\r\n\t\t},\r\n\t\t\"GivenName\": {\r\n\t\t\t\"__field__\": \"firstname\",\r\n\t\t\t\"__type__\": \"string\"\r\n\t\t}\r\n\t}\r\n}";
        var deserialized = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, dynamic>>>(json);
    List<object> values = deserialized.SelectMany(result => result.Value).Cast<object>().ToList();
    

    如果你想要单独的内部类

    public class Client
    {
        public string __entityName__ { get; set; }
        public Dictionary<string, string> ClientId { get; set; }
        public Dictionary<string, string> GivenName { get; set; }
    }
    var deserializedWithClass = JsonConvert.DeserializeObject<Dictionary<string, Client>>(json);
    

    【讨论】:

    • 我尝试过动态的,但如果可能的话,我希望它是一个类或类似的东西。我希望该类有方法,而动态数据类型在 Visual Studio 中没有 IntelliSense。
    • 用类而不是动态更新答案
    • 把根变成一个类是另一种方式。但我认为它必须是一个字典/列表,因为根可能并不总是一个客户端。可以说,一个具有不同内部类的案例,但它们具有相同的字段。我已经更新了原始帖子 JSON 以显示我的意思。
    【解决方案2】:

    将嵌套的 JSON 反序列化为 Class。不是基于字典,但它很有用。

    步骤01:打开链接https://jsonformatter.org/json-parser

    步骤02:复制下来的内容。

    {
    "Client": {
        "__class__": "contact",
    
        "ClientId": {
            "__field__": "new_ndisclientid",
            "__type__": "string"
        },
        "GivenName": {
            "__field__": "firstname",
            "__type__": "string"
        }
    },
    "Case": {
        "__class__": "contact",
    
        "CaseId": {
            "__field__": "new_ndiscaseid",
            "__type__": "string"
        }
    }
    }
    

    步骤 03:打开上面的链接。复制内容并粘贴到左侧,然后单击 JSON Parser 按钮。如下图所示。

    步骤04:点击下载按钮。下载 jsonformatter.txt 文件。成功下载文件为 jsonformatter.txt。

    步骤 05:复制步骤 02 的内容并打开 url https://json2csharp.com/。复制内容并粘贴到左侧,然后单击转换按钮。如下图所示。

    步骤 06:在脚本中。

    (A) 创建 myRootClass.cs 文件并将内容复制并粘贴到您的文件中。[[System.Serializable] 它用于统一 3d 软件 c# 脚本]

    [System.Serializable]
    public class myRootClass
    {
        [System.Serializable]
        public class ClientId
        {
            public string __field__ { get; set; }
            public string __type__ { get; set; }
        }
        [System.Serializable]
        public class GivenName
        {
            public string __field__ { get; set; }
            public string __type__ { get; set; }
        }
        [System.Serializable]
        public class Client
        {
            public string __class__ { get; set; }
            public ClientId ClientId { get; set; }
            public GivenName GivenName { get; set; }
        }
        [System.Serializable]
        public class CaseId
        {
            public string __field__ { get; set; }
            public string __type__ { get; set; }
        }
        [System.Serializable]
        public class Case
        {
            public string __class__ { get; set; }
            public CaseId CaseId { get; set; }
        }
        [System.Serializable]
        public class Root
        {
            public Client Client { get; set; }
            public Case Case { get; set; }
        }
    }
    
        
    

    (B) 读取 jsonformatter.txt 文件。

    // Read entire text file content in one string 
        string  textFilePath = "C:/Users/XXX/Downloads/jsonformatter.txt";//change path
        string jsontext = System.IO.File.ReadAllText(textFilePath);  
        Debug.Log("Read Json"+jsontext);// used Console.Writeline
    

    (C) 将字符串转换成C#并显示数据。

      Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(jsontext); 
      var client = myDeserializedClass.Client;
      Debug.Log("client.__class__ :- "+client.__class__); //used Console.Writeline
      Debug.Log("client.ClientId.__field__ :- "+client.ClientId.__field__);// used Console.Writeline
      Debug.Log("client.GivenName.__field__ :- "+client.GivenName.__field__);// used Console.Writeline
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-22
      • 1970-01-01
      • 2018-09-03
      • 1970-01-01
      • 2022-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多