All Code As follows.
Copy code The Code is as follows: // <summary>
/// JSON parsing class
/// </Summary>
Public static class jsonconvert
{
# Region global variables
Private Static jsonobject _ JSON = new jsonobject (); // register
Private Static readonly string _ semicolon = "@ Semicolon"; // semicolon Escape Character
Private Static readonly string _ comma = "@ comma"; // comma Escape Character
# Endregion
# Region string escape
/// <Summary>
/// Escape the string and convert the "and" in double quotation marks into "_ Semicolon" and "_ comma" respectively.
/// </Summary>
/// <Param name = "text"> </param>
/// <Returns> </returns>
Private Static string strencode (string text)
{
Matchcollection matches = RegEx. Matches (text, "\\\" [^\\ "] + \\\"");
Foreach (match in matches)
{
TEXT = text. Replace (match. Value, match. value. Replace (":", _ semicolon). Replace (",", _ comma ));
}
Return text;
}
/// <Summary>
/// String escape, convert _ semicolon and _ comma into: And,
/// </Summary>
/// <Param name = "text"> </param>
/// <Returns> </returns>
Private Static string strdecode (string text)
{
Return text. Replace (_ semicolon, ":"). Replace (_ comma ,",");
}
# Endregion
# Region JSON minimum unit Parsing
/// <Summary>
/// Convert the minimum object to jsonobject
/// </Summary>
/// <Param name = "text"> </param>
/// <Returns> </returns>
Private Static jsonobject deserializesingletonobject (string text)
{
Jsonobject = new jsonobject ();
Matchcollection matches = RegEx. Matches (text ,"(\\\"(? <Key> [^ \ "] + )\\\":\\\"(? <Value> [^, \\\ "] +) \\\") | (\\\"(? <Key> [^ \\\ "] + )\\\":(? <Value> [^, \ "\}] + ))");
Foreach (match in matches)
{
String value = match. Groups ["value"]. value;
Jsonobject. Add (match. Groups ["key"]. Value, _ JSON. containskey (value )? _ JSON [value]: strdecode (value ));
}
Return jsonobject;
}
/// <Summary>
/// Convert the minimum array to jsonarray
/// </Summary>
/// <Param name = "text"> </param>
/// <Returns> </returns>
Private Static jsonarray deserializesingletonarray (string text)
{
Jsonarray = new jsonarray ();
Matchcollection matches = RegEx. Matches (text ,"(\\\"(? <Value> [^, \ "] +) \") | (? <Value> [^, \ [\] + )");
Foreach (match in matches)
{
String value = match. Groups ["value"]. value;
Jsonarray. Add (_ JSON. containskey (value )? _ JSON [value]: strdecode (value ));
}
Return jsonarray;
}
/// <Summary>
/// Deserialization
/// </Summary>
/// <Param name = "text"> </param>
/// <Returns> </returns>
Private Static string deserialize (string text)
{
TEXT = strencode (text); // escape; and,
Int COUNT = 0;
String key = string. empty;
String Pattern = "(\ {[^ \ [\] \ {\}] + \\}) | (\ [[^ \ [\] \ {\}] + \]) ";
While (RegEx. ismatch (text, pattern ))
{
Matchcollection matches = RegEx. Matches (text, pattern );
Foreach (match in matches)
{
Key = "___ key" + Count + "___";
If (match. value. substring (0, 1) = "{")
_ JSON. Add (Key, deserializesingletonobject (match. Value ));
Else
_ JSON. Add (Key, deserializesingletonarray (match. Value ));
TEXT = text. Replace (match. Value, key );
Count ++;
}
}
Return text;
}
# Endregion
# Region Public interface
/// <Summary>
/// Serialize A jsonobject
/// </Summary>
/// <Param name = "text"> </param>
/// <Returns> </returns>
Public static jsonobject deserializeobject (string text)
{
_ JSON = new jsonobject ();
Return _ JSON [deserialize (text)] as jsonobject;
}
/// <Summary>
/// Serialize the jsonarray object
/// </Summary>
/// <Param name = "text"> </param>
/// <Returns> </returns>
Public static jsonarray deserializearray (string text)
{
_ JSON = new jsonobject ();
Return _ JSON [deserialize (text)] as jsonarray;
}
///
// deserialize the jsonobject
///
//
//
Public static string serializeobject (jsonobject)
{< br> stringbuilder sb = new stringbuilder ();
Sb. append ("{");
foreach (keyvaluepair kVp in jsonobject)
{< br> If (kVp. value is jsonobject)
{< br> Sb. append (string. format ("\" {0} \ ": {1},", kVp. key, serializeobject (jsonobject) kVp. value);
}< br> else if (kVp. value is jsonarray)
{< br> Sb. append (string. format ("\" {0} \ ": {1},", kVp. key, serializearray (jsonarray) kVp. value);
}< br> else if (kVp. value is string)
{< br> Sb. append (string. format ("\" {0} \ ": \" {1} \ ",", kVp. key, kVp. value);
}< br> else
{< br> Sb. append (string. format ("\" {0} \ ": \" {1} \ ",", kVp. key, "");
}< BR >}< br> If (sb. length> 1)
Sb. remove (sb. length-1, 1);
Sb. append ("}");
return sb. tostring ();
}
/// <Summary>
/// Deserialize the jsonarray object
/// </Summary>
/// <Param name = "jsonarray"> </param>
/// <Returns> </returns>
Public static string serializearray (jsonarray)
{
Stringbuilder sb = new stringbuilder ();
SB. append ("[");
For (INT I = 0; I <jsonarray. Count; I ++)
{
If (jsonarray [I] Is jsonobject)
{
SB. append (string. Format ("{0},", serializeobject (jsonobject) jsonarray [I]);
}
Else if (jsonarray [I] Is jsonarray)
{
SB. append (string. Format ("{0},", serializearray (jsonarray) jsonarray [I]);
}
Else if (jsonarray [I] is string)
{
SB. append (string. Format ("\" {0} \ ",", jsonarray [I]);
}
Else
{
SB. append (string. Format ("\" {0 }\",",""));
}
}
If (sb. length> 1)
SB. Remove (sb. Length-1, 1 );
SB. append ("]");
Return sb. tostring ();
}
# Endregion
}
/// <Summary>
/// Retrieve the JSON object class
/// </Summary>
Public class jsonobject: dictionary <string, Object>
{
Public new void add (string key, object value)
{
System. type T = value. GetType ();
If (T. Name = "string ")
{
Value = jsonencode. strencodefordeserialize (value. tostring ());
}
Base. Add (Key, value );
}
}
/// <Summary>
/// Retrieve the JSON array class
/// </Summary>
Public class jsonarray: List <Object>
{
Public new void add (Object item)
{
System. type T = item. GetType ();
If (T. Name = "string ")
{
Item = jsonencode. strencodefordeserialize (item. tostring ());
}
Base. Add (item );
}
}
///
// string escape, set "{", "}", ""
///
public class jsonencode
{< br> Public static readonly string _ leftbraces = "@ leftbraces "; // "{" Escape Character
Public static readonly string _ rightbraces = "@ rightbraces "; // "}" Escape Character
Public static readonly string _ leftbrackets = "@ leftbrackets "; // "[" Escape Character
Public static readonly string _ rightbrackets = "@ rightbrackets "; // "]" Escape Character
Public static readonly string _ doublequotationmarks = "@ doublequotationmarks"; // "Escape Character
# region string escape
///
// string escape "{","}",""", convert _ leftbraces, _ rightbraces, and _ doublequotationmarks
///
///
///
Public static string strencodefordeserialize (string text)
{< br> return text
. replace ("{", _ leftbraces)
. replace ("}", _ rightbraces)
. replace ("[", _ leftbrackets)
. replace ("]", _ rightbrackets)
. replace ("\" ", _ doublequotationmarks);
}
/// <Summary>
/// Escape the string and convert _ leftbraces, _ rightbraces, and _ doublequotationmarks to "{", "}", and "respectively "{","}","""
/// </Summary>
/// <Param name = "text"> </param>
/// <Returns> </returns>
Public static string strdecodefordeserialize (string text)
{
Return text. Replace (_ leftbraces ,"{")
. Replace (_ rightbraces ,"}")
. Replace (_ leftbrackets ,"[")
. Replace (_ rightbrackets, "]")
. Replace (_ doublequotationmarks ,"\"");
}
# Endregion
}
The last thing I want to talk about is that the following method is used to retrieve the value in JSON.Copy codeThe Code is as follows: This. label2.text = jsonencode. strdecodefordeserialize (JSON ["Domain"]. tostring ());
This. label2.text = jsonencode. strdecodefordeserialize (jsonarray) JSON ["years"]) [4]. tostring ());