将嵌套的 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