asp.net 2.0里也可以用JSON的使用方法
程序员文章站
2024-03-07 21:19:15
全部代码如下。 复制代码 代码如下:/// /// json解析类 /// public sta...
全部代码如下。
/// <summary>
/// json解析类
/// </summary>
public static class jsonconvert
{
#region 全局变量
private static jsonobject _json = new jsonobject();//寄存器
private static readonly string _semicolon = "@semicolon";//分号转义符
private static readonly string _comma = "@comma"; //逗号转义符
#endregion
#region 字符串转义
/// <summary>
/// 字符串转义,将双引号内的:和,分别转成_semicolon和_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>
/// 字符串转义,将_semicolon和_comma分别转成:和,
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
private static string strdecode(string text)
{
return text.replace(_semicolon, ":").replace(_comma, ",");
}
#endregion
#region json最小单元解析
/// <summary>
/// 最小对象转为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>
/// 最小数组转为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>
/// 反序列化
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
private static string deserialize(string text)
{
text = strencode(text);//转义;和,
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 公共接口
/// <summary>
/// 序列化jsonobject对象
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public static jsonobject deserializeobject(string text)
{
_json = new jsonobject();
return _json[deserialize(text)] as jsonobject;
}
/// <summary>
/// 序列化jsonarray对象
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public static jsonarray deserializearray(string text)
{
_json = new jsonobject();
return _json[deserialize(text)] as jsonarray;
}
/// <summary>
/// 反序列化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>
/// 反序列化jsonarray对象
/// </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>
/// 取出json对象类
/// </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>
/// 取出json数组类
/// </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>
/// 字符串转义,将"{"、"}"、"""
/// </summary>
public class jsonencode
{
public static readonly string _leftbraces = "@leftbraces";//"{"转义符
public static readonly string _rightbraces = "@rightbraces";//"}"转义符
public static readonly string _leftbrackets = "@leftbrackets";//"["转义符
public static readonly string _rightbrackets = "@rightbrackets";//"]"转义符
public static readonly string _doublequotationmarks = "@doublequotationmarks";//"""转义符
#region 字符串转义
/// <summary>
/// 字符串转义,将"{"、"}"、""",分别转换_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>
/// 字符串转义,将_leftbraces、_rightbraces、_doublequotationmarks,分别转换"{"、"}"、"""
/// </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
}
最后要说的,就是比较马烦的是,现在要取json里的值要用到下面的方法
this.label2.text = jsonencode.strdecodefordeserialize(json["domain"].tostring());
this.label2.text = jsonencode.strdecodefordeserialize(((jsonarray)json["years"])[4].tostring());
复制代码 代码如下:
/// <summary>
/// json解析类
/// </summary>
public static class jsonconvert
{
#region 全局变量
private static jsonobject _json = new jsonobject();//寄存器
private static readonly string _semicolon = "@semicolon";//分号转义符
private static readonly string _comma = "@comma"; //逗号转义符
#endregion
#region 字符串转义
/// <summary>
/// 字符串转义,将双引号内的:和,分别转成_semicolon和_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>
/// 字符串转义,将_semicolon和_comma分别转成:和,
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
private static string strdecode(string text)
{
return text.replace(_semicolon, ":").replace(_comma, ",");
}
#endregion
#region json最小单元解析
/// <summary>
/// 最小对象转为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>
/// 最小数组转为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>
/// 反序列化
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
private static string deserialize(string text)
{
text = strencode(text);//转义;和,
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 公共接口
/// <summary>
/// 序列化jsonobject对象
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public static jsonobject deserializeobject(string text)
{
_json = new jsonobject();
return _json[deserialize(text)] as jsonobject;
}
/// <summary>
/// 序列化jsonarray对象
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public static jsonarray deserializearray(string text)
{
_json = new jsonobject();
return _json[deserialize(text)] as jsonarray;
}
/// <summary>
/// 反序列化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>
/// 反序列化jsonarray对象
/// </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>
/// 取出json对象类
/// </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>
/// 取出json数组类
/// </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>
/// 字符串转义,将"{"、"}"、"""
/// </summary>
public class jsonencode
{
public static readonly string _leftbraces = "@leftbraces";//"{"转义符
public static readonly string _rightbraces = "@rightbraces";//"}"转义符
public static readonly string _leftbrackets = "@leftbrackets";//"["转义符
public static readonly string _rightbrackets = "@rightbrackets";//"]"转义符
public static readonly string _doublequotationmarks = "@doublequotationmarks";//"""转义符
#region 字符串转义
/// <summary>
/// 字符串转义,将"{"、"}"、""",分别转换_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>
/// 字符串转义,将_leftbraces、_rightbraces、_doublequotationmarks,分别转换"{"、"}"、"""
/// </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
}
最后要说的,就是比较马烦的是,现在要取json里的值要用到下面的方法
复制代码 代码如下:
this.label2.text = jsonencode.strdecodefordeserialize(json["domain"].tostring());
this.label2.text = jsonencode.strdecodefordeserialize(((jsonarray)json["years"])[4].tostring());