Chinaunix首页 | 论坛 | 博客
  • 博客访问: 905477
  • 博文数量: 201
  • 博客积分: 8078
  • 博客等级: 中将
  • 技术积分: 2162
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-20 17:22
文章分类

全部博文(201)

文章存档

2013年(3)

2012年(11)

2011年(34)

2010年(25)

2009年(51)

2008年(77)

分类: WINDOWS

2011-01-06 14:19:50

其实这个代码用处不大,只是提供一个分析JSON处理流程的思路。因为AJAX.net已经提供了复杂类型的支持。

public class JSON
    {
        string keyName = null;
        bool parseError = false;
        object returnObject = null;

        public enum JSObjectType
        {
            JSOBJ, JSINT, JSSTR, JSNUL, JSBOOL, JSARRAY
        }

        public bool parseSuccess
        {
            get
            {
                return !parseError;
            }
        }

        public object Parse(string json)
        {
            parseError = false;
            ParseJSON(json, 0);

            if (parseError)
            {
                return null;
            }

            return returnObject;
        }

        private int ParseJSON(string json, int index)
        {
            int i = SkipSpace(json, index);

            if (i < json.Length)
            {
                switch (json[i])
                {
                    case '[':
                        i++;

                        List<Object> array = new List<object>();
                        if (i < json.Length && json[i] != ']')
                        {
                            i = ParseJSON(json, i);

                            if (!parseError)
                            {
                                array.Add(returnObject);
                            }

                            while (i < json.Length && json[i] == ',')
                            {
                                i++;
                                i = ParseJSON(json, i);

                                if (!parseError)
                                {
                                    array.Add(returnObject);
                                }
                            }
                        }

                        if (i >= json.Length || json[i] != ']')
                        {
                            parseError = true;
                            break;
                        }

                        returnObject = array;
                        i++;
                        break;

                    case '{':
                        i++;

                        Hashtable hashTable = new Hashtable();
                        if (i < json.Length && json[i] != '}')
                        {
                            i = ParsePair(json, i, hashTable);
                            while (i < json.Length && json[i] == ',')
                            {
                                i++;
                                i = ParsePair(json, i, hashTable);
                            }
                        }

                        if (i >= json.Length || json[i] != '}')
                        {
                            parseError = true;
                            break;
                        }

                        returnObject = hashTable;
                        hashTable = null;
                        i++;
                        break;

                    case '"':
                    case '\'':
                        i = ParseString(json, i);
                        break;

                    default:
                        if (Char.IsDigit(json[i]))
                        {
                            StringBuilder sb = new StringBuilder();
                            while (Char.IsDigit(json[i]))
                            {
                                sb.Append(json[i]);
                                i++;
                            }

                            returnObject = int.Parse(sb.ToString());
                            break;
                        }

                        if (json.Substring(i).StartsWith("true"))
                        {
                            returnObject = true;
                            i += 4;
                            break;
                        }

                        if (json.Substring(i).StartsWith("false"))
                        {
                            returnObject = false;
                            i += 5;
                            break;
                        }

                        if (json.Substring(i).StartsWith("null"))
                        {
                            returnObject = null;
                            i += 4;
                            break;
                        }

                        parseError = true;
                        break;
                }

                i = SkipSpace(json, i);
            }

            return i;
        }

        private int ParsePair(string json, int index, Hashtable table)
        {
            int i = ParseString(json, index);
            string name = this.keyName;

            if (parseError || name == null || name.Length == 0)
            {
                parseError = true;
                return i;
            }

            name = this.keyName;
            if (i >= json.Length || json[i] != ':')
            {
                parseError = true;
                return i;
            }

            i = ParseJSON(json, ++i);
            if (!parseError)
            {
                table[name] = returnObject;
            }

            return i;
        }

        private int SkipSpace(string json, int index)
        {
            while (index < json.Length &&
                Char.IsWhiteSpace(json[index]))
            {
                index++;
            }

            return index;
        }

        private int ParseString(string json, int index)
        {
            Char lastCode;
            int i = SkipSpace(json, index);
            StringBuilder sb = new StringBuilder();

            if (i < json.Length)
            {
                Char start = json[i];

                switch (start)
                {
                    case '\'':
                    case '"':
                        i++;

                        lastCode = '#';
                        while (i < json.Length && json[i] != start && lastCode != '\\')
                        {
                            lastCode = lastCode == '\\' ? '#' : json[i];
                            sb.Append(json[i]);
                            i++;
                        }

                        if (i >= json.Length)
                        {
                            parseError = true;
                            break;
                        }

                        i++;
                        break;

                    default:
                        while (i < json.Length && Char.IsLetterOrDigit(json[i]))
                        {
                            sb.Append(json[i]);
                            i++;
                        }
                        break;
                }

                i = SkipSpace(json, i);
            }

            if (!parseError)
            {
                this.keyName = sb.ToString();
                returnObject = this.keyName;
            }

            return i;
        }

        public JSON At(int index)
        {
            JSON json = new JSON();
            List<object> jsArray = null;
            jsArray = this.returnObject as List<object>;

            if (jsArray != null &&
                index < jsArray.Count)
            {
                json.returnObject = jsArray[index];
            }

            return json;
        }

        public JSON At(string name)
        {
            JSON json = new JSON();
            Hashtable jsTable = null;
            jsTable = this.returnObject as Hashtable;

            if (jsTable != null &&
                jsTable.ContainsKey(name))
            {
                json.returnObject = jsTable[name];
            }

            return json;
        }

        public bool ToBool()
        {
            return (bool)this.returnObject;
        }

        public int ToValue()
        {
            return (int)this.returnObject;
        }

        public string ToText()
        {
            return this.returnObject as string;
        }

        public int Length
        {
            get
            {
                if (this.JSType == JSObjectType.JSARRAY)
                    return (this.returnObject as List<Object>).Count;

                return 0;
            }
        }

        public JSObjectType JSType
        {
            get
            {
                if (this.returnObject == null)
                    return JSObjectType.JSNUL;

                if (returnObject.GetType() == typeof(int))
                    return JSObjectType.JSINT;

                if (returnObject.GetType() == typeof(string))
                    return JSObjectType.JSSTR;

                if (returnObject.GetType() == typeof(bool))
                    return JSObjectType.JSBOOL;

                if (returnObject.GetType() == typeof(List<object>))
                    return JSObjectType.JSARRAY;

                if (returnObject.GetType() == typeof(Hashtable))
                    return JSObjectType.JSOBJ;

                return JSObjectType.JSNUL;
            }
        }

        public JSON Attribute(string name)
        {
            Hashtable jsTable = null;
            jsTable = this.returnObject as Hashtable;

            if (jsTable != null &&
                jsTable.ContainsKey(name))
            {
                JSON json = new JSON();
                json.returnObject = jsTable[name];
                return json;
            }

            return null;
        }

        public JSON IndexOf(int index)
        {
            List<object> jsArray = null;
            jsArray = this.returnObject as List<object>;

            if (jsArray != null &&
                index < jsArray.Count)
            {
                JSON json = new JSON();
                json.returnObject = jsArray[index];
                return json;
            }

            return null;
        }
    }


阅读(1317) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~