[C#] 纯文本查看 复制代码
public class JSONConvert
{
#region 全局变量
private JSONObject _json = new JSONObject();//寄存器
private readonly string _SEMICOLON = "@semicolon";//分号转义符
private readonly string _COMMA = "@comma"; //逗号转义符
#endregion
#region 字符串转义
/// <summary>
/// 字符串转义,将双引号内的:和,分别转成_SEMICOLON和_COMMA
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
private string StrEncode(string text)
{
System.Text.RegularExpressions.MatchCollection matches = System.Text.RegularExpressions.Regex.Matches(text, "\\\"[^\\\"]+\\\"");
foreach (System.Text.RegularExpressions.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 string StrDecode(string text)
{
return text.Replace(_SEMICOLON, ":").Replace(_COMMA, ",");
}
#endregion
#region JSON最小单元解析
/// <summary>
/// 最小对象转为JSONObject
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
private JSONObject DeserializeSingletonObject(string text)
{
JSONObject jsonObject = new JSONObject();
System.Text.RegularExpressions.MatchCollection matches = System.Text.RegularExpressions.Regex.Matches(text, "(\\\"(?<key>[^\\\"]+)\\\":\\\"(?<value>[^,\\\"]*)\\\")|(\\\"(?<key>[^\\\"]+)\\\":(?<value>[^,\\\"\\}]*))");
foreach (System.Text.RegularExpressions.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 JSONArray DeserializeSingletonArray(string text)
{
JSONArray jsonArray = new JSONArray();
System.Text.RegularExpressions.MatchCollection matches = System.Text.RegularExpressions.Regex.Matches(text, "(\\\"(?<value>[^,\\\"]+)\")|(?<value>[^,\\[\\]]+)");
foreach (System.Text.RegularExpressions.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 string Deserialize(string text)
{
text = StrEncode(text);//转义;和,
int count = 0;
string key = string.Empty;
string pattern = "(\\{[^\\[\\]\\{\\}]+\\})|(\\[[^\\[\\]\\{\\}]+\\])";
while (System.Text.RegularExpressions.Regex.IsMatch(text, pattern))
{
System.Text.RegularExpressions.MatchCollection matches = System.Text.RegularExpressions.Regex.Matches(text, pattern);
foreach (System.Text.RegularExpressions.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));
//foreach (var item in _json)
//{
// text = item.Key + ":" + item.Value+",";
//}
text = text.Replace(match.Value, key);//.Replace(match.Value, key)
count++;
}
}
return text;
}
#endregion
#region 公共接口
/// <summary>
/// 序列化JSONObject对象
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public JSONObject DeserializeObject(string text)
{
string res = string.Empty;
if (text.Contains('('))
{
res = Stringoperation.Between(text, "(", ")");
return _json[Deserialize(res)] as JSONObject;
}
else
{
return _json[Deserialize(text)] as JSONObject;
}
}
/// <summary>
/// 序列化JSONArray对象
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public JSONArray DeserializeArray(string text)
{
string res = string.Empty;
if (text.Contains('('))
{
res = Stringoperation.Between(text, "(", ")");
return _json[Deserialize(res)] as JSONArray;
}
else
{
return _json[Deserialize(text)] as JSONArray;
}
}
/// <summary>
/// 反序列化JSONObject对象
/// </summary>
/// <param name="jsonObject"></param>
/// <returns></returns>
public 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 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>
/// 类 名:JSONObject
/// 描 述:JSON对象类
/// 继承Dictionary<TKey, TValue>代替this[]
/// </summary>
public class JSONObject : Dictionary<string, object>
{ }
/// <summary>
/// 类 名:JSONArray
/// 描 述:JSON数组类
///继承List<T>代替this[]
/// </summary>
public class JSONArray : List<object>
{ }