ASP.net 2.0 can also use the JSON method _ practical Tips

Source: Internet
Author: User
Tags serialization static class
All the code is as follows.
Copy Code code as follows:

<summary>
JSON parsing class
</summary>
public static Class JsonConvert
{
#region Global Variables

private static Jsonobject _json = new Jsonobject ();//Registers
private static readonly string _semicolon = "@semicolon";//semicolon escape character
private static readonly string _comma = "@comma"; Comma escape character

#endregion

#region String Escape
<summary>
String escape, enclose in double quotes: and, respectively, into _semicolon and _comma
</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, converting _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 cell resolution

<summary>
The smallest object is converted 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>
The smallest array is converted 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;

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>
Serialization of Jsonobject objects
</summary>
<param name= "Text" ></param>
<returns></returns>
public static Jsonobject Deserializeobject (string text)
{
_json = new Jsonobject ();
return _json[deserialize (text)] as Jsonobject;
}

<summary>
Serialization of Jsonarray objects
</summary>
<param name= "Text" ></param>
<returns></returns>
public static Jsonarray Deserializearray (string text)
{
_json = new Jsonobject ();
return _json[deserialize (text)] as Jsonarray;
}

<summary>
Deserialize a Jsonobject object
</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 a 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>
Remove 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>
Remove 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>
String escape, "{", "}", "" "
</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>
String escape, Convert "{", "}", "" "_leftbraces, _rightbraces, _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>
String escape, converts _leftbraces, _rightbraces, _doublequotationmarks, 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 to say is that the more annoying thing is, now to take the value of the JSON to use the following method
Copy Code code as follows:

This. Label2.Text = jsonencode.strdecodefordeserialize (json["domain"). ToString ());
This. Label2.Text = Jsonencode.strdecodefordeserialize ((jsonarray) json["Years"]) [4]. ToString ());
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.