.NET FrameWork 2.0 ²¢Ã»ÓÐÌṩJSON ×Ö·û´®¶ÔÏ󻯹¤¾ß£¬Òò´Ë³¢ÊÔдÁËÕâ¸öת»»Æ÷£¬Ä¿Ç°ÒÑͶÈëʹÓ㬷ÖÏíһϡ£ÊµÏÖ·½Ê½ÊÇ£ºÕýÔò + µÝ¹é£¬ ¶ÔÐèҪת»»µÄJson ×Ö·û´®¸´ÔÓ¶ÈûÓÐÒªÇ󣬻¶Ó²âÊÔ£¬²¢Ìṩ·´À¡,£¬Ð»Ð»¡£ µÚÒ»´ÎÔËÐУ¬ÓеãÂý£¬¹À¼ÆÊdzõʹ»¯ÕýÔòÕ¼ÓÃÁËʱ¼ä£¬ÕâЩÕýÔòÊǾ²Ì¬µÄ£¬Ö®ºóµÄת»»»á¼Ó¿ì¡£
/*create by ayymbirst @gmail.com */ using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressions; namespace JsonConver { /// <summary> /// ½Úµãö¾Ù /// </summary> public enum NodeType { /// <summary> /// ±êʶÊý×é /// </summary> IsArray , /// <summary> /// ±êʶ¶ÔÏó /// </summary> IsObject , /// <summary> /// ±êʶԪÊý¾Ý /// </summary> IsOriginal , /// <summary> /// δ֪¸ñʽ /// </summary> Undefined } //ÃèÊöJson½Úµã public class JsonNode { public NodeType NodeType; public List<JsonNode> List; public Dictionary<string, JsonNode> DicObject; public string Value; } /// <summary> /// json ×Ö·û´®¶ÔÏó»¯ /// </summary> public class ConvertJsonObject { static string regTxt = "({0}[^{0}{1}]*(((?'Open'{0})[^{0}{1}]*)+((?'-Open'{1})[^{0}{1}]*)+)*(?(Open)(?!)){1})"; //Æ¥Åä×Ö·û´®(µ¥Ë«ÒýºÅ·¶Î§) static string regKeyValue = "({0}.{1}?(?<!\\\\){0})"; //ÅжÏÊÇ·ñ°üº¬µ¥,Ë«ÒýºÅ //Æ¥ÅäÔªÊý¾Ý(²»°üº¬¶ÔÏó,Êý×é) static string regOriginalValue = string.Format("({0}|{1}|{2})", string.Format(regKeyValue, "'", "*"), string.Format(regKeyValue, "\"", "*"), "\\w+"); //Æ¥Åävalue (°üº¬¶ÔÏóÊý×é) static string regValue = string.Format("({0}|{1}|{2})", regOriginalValue //×Ö·û , string.Format(regTxt, "\\[", "\\]"), string.Format(regTxt, "\\{", "\\}")); //Æ¥Åä¼üÖµ¶Ô static string regKeyValuePair = string.Format("\\s*(?<key>{0}|{1}|{2})\\s*:\\s*(?<value>{3})\\s*" , string.Format(regKeyValue, "'", "+"), string.Format(regKeyValue, "\"", "+"), "([^ :,]+)" //Æ¥Åäkey , regValue); //Æ¥Åävalue /// <summary> /// ÅжÏÊÇ·ñÊǶÔÏó /// </summary> static Regex RegJsonStrack1 = new Regex(string.Format("^\\{0}(({2})(,(?=({2})))?)+\\{1}{1}quot;, "{", "}", regKeyValuePair), RegexOptions.Compiled); /// <summary> /// ÅжÏÊÇ·ñÊÇÐòÁÐ /// </summary> static Regex RegJsonStrack2 = new Regex(string.Format("^\\[(({0})(,(?=({0})))?)+\\]{1}quot;, regValue), RegexOptions.Compiled); /// <summary> /// ÅжϼüÖµ¶Ô /// </summary> static Regex RegJsonStrack3 = new Regex(regKeyValuePair, RegexOptions.Compiled); //Æ¥Åävalue static Regex RegJsonStrack4 = new Regex(regValue, RegexOptions.Compiled); //Æ¥ÅäÔªÊý¾Ý static Regex RegJsonStrack6 = new Regex(string.Format("^{0}{1}quot;, regOriginalValue), RegexOptions.Compiled); //ÒÆ³ýÁ½¶Ë[] , {} static Regex RegJsonRemoveBlank = new Regex("(^\\s*[\\[\\{'\"]\\s*)|(\\s*[\\]\\}'\"]\\s*$)", RegexOptions.Compiled); string JsonTxt; public ConvertJsonObject(string json) { //È¥µô»»Ðзû json = Regex.Replace(json, "[\r\n]", ""); JsonTxt = json; } /// <summary> /// ÅжϽڵãÄÚÐÍ /// </summary> /// <param name="json"></param> /// <returns></returns> public NodeType MeasureType(string json) { if (RegJsonStrack1.IsMatch(json)) { return NodeType.IsObject; } if (RegJsonStrack2.IsMatch(json)) { return NodeType.IsArray; } if (RegJsonStrack6.IsMatch(json)) { return NodeType.IsOriginal; } return NodeType.Undefined; } /// <summary> /// json ×Ö·û´®ÐòÁл¯Îª¶ÔÏó /// </summary> /// <param name="json"></param> /// <returns></returns> public JsonNode SerializationJsonNodeToObject() { return SerializationJsonNodeToObject(JsonTxt); } /// <summary> /// json ×Ö·û´®ÐòÁл¯Îª¶ÔÏó /// </summary> /// <param name="json"></param> /// <returns></returns> public JsonNode SerializationJsonNodeToObject(string json) { json = json.Trim(); NodeType nodetype = MeasureType(json); if (nodetype == NodeType.Undefined) { throw new Exception("δ֪¸ñʽJson: " + json); } JsonNode newNode = new JsonNode(); newNode.NodeType = nodetype; if (nodetype == NodeType.IsArray) { json = RegJsonRemoveBlank.Replace(json, ""); MatchCollection matches = RegJsonStrack4.Matches(json); newNode.List = new List<JsonNode>(); foreach (Match match in matches) { if (match.Success) { newNode.List.Add(SerializationJsonNodeToObject(match.Value)); } } } else if (nodetype == NodeType.IsObject) { json = RegJsonRemoveBlank.Replace(json, ""); MatchCollection matches = RegJsonStrack3.Matches(json); newNode.DicObject = new Dictionary<string, JsonNode>(); string key; foreach (Match match in matches) { if (match.Success) { key = RegJsonRemoveBlank.Replace(match.Groups["key"].Value, ""); if (newNode.DicObject.ContainsKey(key)) { throw new Exception("json Êý¾ÝÖаüº¬Öظ´¼ü, json:" + json); } newNode.DicObject.Add(key, SerializationJsonNodeToObject(match.Groups["value"].Value)); } } } else if (nodetype == NodeType.IsOriginal) { newNode.Value = RegJsonRemoveBlank.Replace(json, "").Replace("\\r\\n", "\r\n"); } return newNode; } } }ÆäÖÐ JsonNode ÊÇ·µ»Ø½âÎö½á¹û¡£
NodeType ÊÇö¾ÙÀàÐÍ£¬±íʾµ±Ç°½ÚµãÊÇʲôÀàÐÍ¡£
IsArray: JsonNode.List
IsObject:JsonNode.DicObject
IsOriginal:JsonNode.Value
Json ×Ö·û´®»»ÐÐÇëÓÃ˫б¸Ü£¬Èç "aa\\r\\nbb"£¬±íʾaa,bb ΪÏàÁÚÁ½ÐС£
¡¡