/// <summary>
    /// JSON 的摘要说明。
    /// </summary>
    public class JSON
    {
        private static readonly string Json_A = "3A3B1A16-F697-4a32-A0B0-2515BB4692F5";
        private static readonly string Json_B = "274BEBC4-2482-4426-9A23-C32D25F7615B";
        private static readonly string Json_C = "5ACEB051-8505-4e8c-ACE7-91DE1D101190";
        private static readonly string Json_D = "DEF0006D-650D-497c-9D27-40A20E337970";

        public static object JsonDecode(string srcString)
        {
            srcString = srcString.Trim();
            object result = null;

            srcString = srcString.Replace("},{", Json_A);
            srcString = srcString.Replace("],[", Json_B);
            srcString = srcString.Replace("\",", Json_D);
            srcString = srcString.Replace(",", Json_C);
            srcString = srcString.Replace(Json_D, "\",");
            srcString = srcString.Replace(Json_B, "],[");
            srcString = srcString.Replace(Json_A, "},{");

            if (!(srcString == null || srcString == ""))//不空
            {
                if (srcString.StartsWith("[") && srcString.EndsWith("]"))//序列
                {
                    srcString = srcString.Substring(1, srcString.Length - 2);
                    ArrayList al = new ArrayList();
                    string[] src = srcString.Split(',');
                    foreach (string s in src)
                    {
                        if (s == null || s == "") continue;
                        string ss=s.Replace(Json_C, ",");
                        al.Add(JsonDecode(ss));
                    }

                    result = al;
                }
                else if (srcString.StartsWith("{") && srcString.EndsWith("}"))//映射
                {
                    srcString = srcString.Substring(1, srcString.Length - 2);
                    Hashtable table = new Hashtable();
                    string[] src = srcString.Split(new char[]{','});
                    foreach (string s in src)
                    {
                        string ss = s.Trim().Replace(Json_C, ",");

                        if (ss == null || ss == "") continue;

                        string key = ss.Trim().Substring(ss.IndexOf('"')+1, ss.IndexOf(':') - ss.IndexOf('"')-2);
                        string @value = ss.Trim().Substring(ss.Trim().IndexOf(':') + 1);
                        table.Add(key, JsonDecode(@value));
                    }
                    result = table;
                }
                else //标量
                {
                    srcString=srcString.Replace(Json_C, ",");

                    if ((srcString.StartsWith("'") && srcString.EndsWith("'")) || (srcString.StartsWith("\"") && srcString.EndsWith("\"")))//字符串或时间
                    {
                        try
                        {
                            result = DateTime.Parse(srcString.Substring(0, srcString.Length - 2));//时间
                        }
                        catch//字符串
                        {
                            result = srcString.Substring(1, srcString.Length - 2);
                        }
                    }
                    else if (srcString.IndexOf(".") >= 0)//精度数
                    {
                        try
                        {
                            result = Double.Parse(srcString);
                        }
                        catch//无效数据
                        {
                            result = srcString.Substring(1, srcString.Length - 2);
                        }
                    }
                    else//整数或Bool
                    {
                        try
                        {
                            result = long.Parse(srcString);
                        }
                        catch//Bool 或无效数据
                        {
                            srcString = srcString.ToUpper();

                            if (srcString == "TRUE") result = true;
                            else if (srcString == "FALSE") result = false;
                            else result = null;
                        }
                    }
                }
            }

            return result;
        }
    }
posted on 2013-06-07 15:02  王洪洪  阅读(295)  评论(0编辑  收藏  举报