簡體   English   中英

將嵌套的 JSON 反序列化為字典、字符串和類

[英]Deserialize nested JSON into Dictionary, string and class

我有一個 JSON,我想將其 DeserializeObject 轉換為具有 innerDictionary 和innermostClass 的outerDictionary,如下所示:

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 來做到這一點?

編輯:為了清楚起見,將 classname 重命名為 entityName。 ClientId 和 GivenName 將被反序列化為 fieldClasses。

您可以使用動態或對象代替內部類

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

將嵌套的 JSON 反序列化為類。 不是基於字典,但它很有用。

步驟 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 步的內容並打開網址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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM