全部代码如下。
复制代码 代码如下:
/// <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());
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。
更新日志
2024年11月17日
2024年11月17日
- 【雨果唱片】中国管弦乐《鹿回头》WAV
- APM亚流新世代《一起冒险》[FLAC/分轨][106.77MB]
- 崔健《飞狗》律冻文化[WAV+CUE][1.1G]
- 罗志祥《舞状元 (Explicit)》[320K/MP3][66.77MB]
- 尤雅.1997-幽雅精粹2CD【南方】【WAV+CUE】
- 张惠妹.2007-STAR(引进版)【EMI百代】【WAV+CUE】
- 群星.2008-LOVE情歌集VOL.8【正东】【WAV+CUE】
- 罗志祥《舞状元 (Explicit)》[FLAC/分轨][360.76MB]
- Tank《我不伟大,至少我能改变我。》[320K/MP3][160.41MB]
- Tank《我不伟大,至少我能改变我。》[FLAC/分轨][236.89MB]
- CD圣经推荐-夏韶声《谙2》SACD-ISO
- 钟镇涛-《百分百钟镇涛》首批限量版SACD-ISO
- 群星《继续微笑致敬许冠杰》[低速原抓WAV+CUE]
- 潘秀琼.2003-国语难忘金曲珍藏集【皇星全音】【WAV+CUE】
- 林东松.1997-2039玫瑰事件【宝丽金】【WAV+CUE】