Json解析类
定义两个辅助类
public class JSONObject : Dictionary<string, object>
{ } public class JSONArray : List<object>
{ }
琮义两个辅助类
序列化
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 = ;
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(, ) == "{")
_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)
{
return _json[Deserialize(text)] as JSONObject;
}
/// <summary>
/// 序列化JSONArray对象
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public static JSONArray DeserializeArray(string text)
{
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 > )
sb.Remove(sb.Length - , );
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 = ; 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 > )
sb.Remove(sb.Length - , );
sb.Append("]");
return sb.ToString();
}
#endregion #region 在4.0下添加引用System.Web.Extensions
public static string JavaScriptSerializerJson(object obj)
{
System.Web.Script.Serialization.JavaScriptSerializer javascriptserializer = new System.Web.Script.Serialization.JavaScriptSerializer();
return System.Web.HttpUtility.UrlEncode(javascriptserializer.Serialize(obj));
}
#endregion
}
Json格式处理
调用示例:
static void Main(string[] args)
{ //序列化
JSONArray _array = new JSONArray();
_array.Add("");
_array.Add("");
_array.Add("");
_array.Add("");
JSONObject _object = new JSONObject();//新建json对象作为内嵌
_object.Add("oneKey", "one");
_object.Add("twoArray", _array);
JSONArray jsonArray = new JSONArray();
jsonArray.Add("");
jsonArray.Add("");
jsonArray.Add("");
jsonArray.Add("");
jsonArray.Add("");
JSONObject jsonObject = new JSONObject();
jsonObject.Add("domain", "mzwu.com");
jsonObject.Add("two", _object);//添加json对象
jsonObject.Add("years", jsonArray);
Console.WriteLine("json序列化为字符串");
Console.WriteLine(JSONConvert.SerializeObject(jsonObject));//执行序列化
//反序列化
JSONObject json = JSONConvert.DeserializeObject("{\"domain\":\"mzwu.com\",\"two\":{\"oneKey\":\"one\",\"twoArray\":[1,2,3,4]},\"years\":[2006,2007,2008,2009,2010]}");//执行反序列化
if (json != null)
{
Console.WriteLine("将json结构的字符串反序列化为json对象并调用");
Console.WriteLine(json["domain"]);
Console.WriteLine(((JSONObject)json["two"])["oneKey"]);
Console.WriteLine(((JSONArray)((JSONObject)json["two"])["twoArray"])[]);
Console.WriteLine(((JSONArray)json["years"])[]);
} List<Person> persons = new List<Person>()
{
new Person(){Name="王雪敏",Age=,Sex="Girl"},
new Person(){Name="黄江苗",Age=,Sex="Boy"},
new Person(){Name="黄晓明",Age=,Sex="Man"}
};
StringBuilder sb = new StringBuilder();
sb.Append("[");
foreach (Person person in persons)
{
sb.Append("{");
sb.AppendFormat("\"Name\":\"{0}\",\"Age\":\"{1}\",\"Sex\":\"{2}\",", person.Name, person.Age, person.Sex);
sb.Append("},");
}
string str = sb.ToString().TrimEnd(',');
str += "]";
Console.WriteLine("---------------------------------------------------------------------");
Console.WriteLine(str); Console.WriteLine("---------------------------------JavaScriptSerializer------------------------------------");
string res = JSONConvert.JavaScriptSerializerJson(persons);
Console.WriteLine(System.Web.HttpUtility.UrlDecode(res));
Console.ReadLine();
}