Deserialize nested JSON into Class. not on dictionary based but it's useful.
Step 01: open the link https://jsonformatter.org/json-parser
Step 02: copy the down contents.
{
"Client": {
"__class__": "contact",
"ClientId": {
"__field__": "new_ndisclientid",
"__type__": "string"
},
"GivenName": {
"__field__": "firstname",
"__type__": "string"
}
},
"Case": {
"__class__": "contact",
"CaseId": {
"__field__": "new_ndiscaseid",
"__type__": "string"
}
}
}
Step 03: Open above link. copy contents and past in to left side and click on to JSON Parser button. Look like below image.
Step 04: Click on download button. Downloading the jsonformatter.txt file. Successfully download the file as jsonformatter.txt.
Step 05: Copy step 02 content and open url https://json2csharp.com/.Copy contents and past in to left side and click on to Convert button. Look like below image.
Step 06: In Scripting.
(A) Create myRootClass.cs file and copy and past down contents to your file.[[System.Serializable] it's used in unity 3d software c# scripting]
[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) Read the jsonformatter.txt file.
// 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) Convert string into C# and show the data.
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