Exemple #1
0
        /// <summary>
        /// 创建Json数组对象
        /// </summary>
        /// <param name="value">数组对象字符串包含[]</param>
        /// <returns></returns>
        public MyHashtable[] CreateJsonArr(string value)
        {
            Regex startR = new Regex(@"[\{]");
            Match startM = startR.Match(value);
            int   n      = 0;

            while (startM.Success)
            {
                string start = "{";
                int    i     = startM.Index;
                while (i < value.Length - 1)
                {
                    i++;
                    if (value[i].ToString() == "\\")
                    {
                        i++;
                        continue;
                    }
                    start = Start(start, value[i]);
                    if (start == "")
                    {
                        break;
                    }
                }
                startM = startR.Match(value, i + 1);
                n++;
            }

            MyHashtable[] myHashtables = new MyHashtable[n];

            startM = startR.Match(value);
            n      = 0;
            while (startM.Success)
            {
                string start = "{";
                int    i     = startM.Index;
                while (i < value.Length - 1)
                {
                    i++;
                    if (value[i].ToString() == "\\")
                    {
                        i++;
                        continue;
                    }
                    start = Start(start, value[i]);
                    if (start == "")
                    {
                        break;
                    }
                }
                string str = value.Substring(startM.Index, i - startM.Index + 1);
                myHashtables[n] = new MyHashtable(str);
                startM          = startR.Match(value, i + 1);
                n++;
            }
            return(myHashtables);
        }
Exemple #2
0
        /// <summary>
        /// 给已有的json对象添加一个键值对
        /// </summary>
        /// <param name="name">json数组名称</param>
        /// <param name="aj">需要添加的json对象</param>
        public void Add(string name, MyHashtable aj)
        {
            MyHashtable[] olds = new MyHashtable[0];
            if (this[name] != null)
            {
                olds = (MyHashtable[])this[name];
            }
            MyHashtable[] news = new MyHashtable[olds.Length + 1];
            for (int i = 0; i < olds.Length; i++)
            {
                news[i] = (MyHashtable)olds[i];
            }
            news[olds.Length] = aj;

            this[name] = news;
        }
Exemple #3
0
        private string JsonToString(MyHashtable j)
        {
            string str = "{";
            int    n   = 0;

            foreach (DictionaryEntry de in j)
            {
                if (n > 0)
                {
                    str += ",";
                }
                n++;
                str += "\"" + de.Key.ToString() + "\"" + ":";
                if (de.Value == null)
                {
                    str += "null";
                }
                else if (de.Value.ToString().Split('.')[de.Value.ToString().Split('.').Length - 1] == "Json[]")
                {
                    MyHashtable[] js = (MyHashtable[])this[de.Key];
                    str += "[";
                    for (int i = 0; i < js.Length; i++)
                    {
                        if (i > 0)
                        {
                            str += ",";
                        }
                        str += JsonToString(js[i]);
                    }
                    str += "]";
                }
                else
                {
                    str += "\"" + de.Value.ToString().Replace("\\", "\\\\").Replace("'", "\\'").Replace("\"", "\\\"") + "\"";

                    string abc = str;
                }
            }
            return((str + "}").Replace("\\", ""));
        }
Exemple #4
0
        /// <summary>
        /// 创建Json对象
        /// </summary>
        /// <param name="value">json字符串</param>
        private void CreateJson(string value)
        {
            if (value == null)
            {
                return;
            }
            else
            {
                //value = LI.TextTools.Html.unescape(value);
            }

            string name  = "";
            Regex  nameR = new Regex(@"[a-zA-Z\'\""]+[0-9]*[a-zA-Z\'\""]*[\s]*[\:]");
            Match  nameM = nameR.Match(value);

            while (nameM.Success)
            {
                name = nameM.Value.Replace("'", "").Replace("\"", "").Replace(":", "");

                //当前名称的下一个对应字母(', ", [, {, 0-9, 以,和}标记为值的开始则表示值为null
                Regex startR = new Regex(@"[0-9'""\[\{]");
                Match startM = startR.Match(value, nameM.Index + nameM.Length);
                //匹配成功

                if (startM.Success)
                {
                    string start = startM.Value;
                    int    i     = startM.Index;
                    while (i < value.Length - 1)
                    {
                        i++;
                        //跳过'\'字符
                        if (value[i].ToString() == "\\")
                        {
                            i++;
                            continue;
                        }
                        char vv = value[i];
                        start = Start(start, vv);
                        if (start == "")
                        {
                            break;
                        }
                    }
                    string str = value.Substring(startM.Index, i - startM.Index + 1);

                    nameM = nameR.Match(value, i + 1);

                    char key = str[0];
                    if (key == '{')
                    {
                        this[name] = new MyHashtable(str);
                    }
                    else if (key == '[')
                    {
                        this[name] = CreateJsonArr(str);
                    }
                    else if (key == '\'' || key == '"')
                    {
                        this[name] = str.Substring(1, str.Length - 2).Replace("\\\\", "\\").Replace("\\'", "'").Replace("\\\"", "\"");
                    }
                    else
                    {
                        this[name] = str.Replace(" ", "").Replace("\n", "").Replace("}", "").Replace(",", "").Replace("\r", "");;
                    }
                }
                else
                {
                    Clear();
                    Success = false;
                    return;
                }
            }
            Success = true;
        }