The Code is as follows.
Copy codeThe 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 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 jsonObject = new JSONObject ();
MatchCollection matches = Regex. Matches (text ,"(\\\"(? <Key> [^ \ "] + )\\\":\\\"(? <Value> [^, \\\ "] +) \\\") | (\\\"(? <Key> [^ \\\ "] + )\\\":(? <Value> [^, \ "\}] + ))");
Foreach (Match 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 jsonArray = new JSONArray ();
MatchCollection matches = Regex. Matches (text ,"(\\\"(? <Value> [^, \ "] +) \") | (? <Value> [^, \ [\] + )");
Foreach (Match 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 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;
}
/// <Summary>
/// Deserialize the JSONObject
/// </Summary>
/// <Param name = "jsonObject"> </param>
/// <Returns> </returns>
Public static string SerializeObject (JSONObject jsonObject)
{
StringBuilder sb = new StringBuilder ();
Sb. Append ("{");
Foreach (KeyValuePair <string, object> kvp in jsonObject)
{
If (kvp. Value is JSONObject)
{
Sb. Append (string. Format ("\" {0} \ ": {1},", kvp. Key, SerializeObject (JSONObject) kvp. Value )));
}
Else if (kvp. Value is JSONArray)
{
Sb. Append (string. Format ("\" {0} \ ": {1},", kvp. Key, SerializeArray (JSONArray) kvp. Value )));
}
Else if (kvp. Value is String)
{
Sb. Append (string. Format ("\" {0} \ ": \" {1} \ ",", kvp. Key, kvp. Value ));
}
Else
{
Sb. Append (string. Format ("\" {0} \ ": \" {1} \ ",", kvp. Key ,""));
}
}
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 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 );
}
}
/// <Summary>
/// Escape the string "{","}","""
/// </Summary>
Public class JsonEncode
{
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
/// <Summary>
/// Escape the string and convert "{", "}", "" To _ LEFTBRACES, _ RIGHTBRACES, and _ DOUBLEQUOTATIONMARKS.
/// </Summary>
/// <Param name = "text"> </param>
/// <Returns> </returns>
Public static string StrEncodeForDeserialize (string text)
{
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 ());