Java JSON组成和解析

打印 上一主题 下一主题

主题 868|帖子 868|积分 2604

  本框架JSON元素组成和分析,JsonElement分三大范例JsonArray,JsonObject,JsonString。
  JsonArray:数组和Collection子类,指定命组的话,使用ArrayList来add元素,遍历ArrayList再使用Array.newInstance生成数组并添加元素即可.
  JsonObject:带有泛型的封装类,给带有泛型的字段赋值,关键在于如何根据“指定的范例”和“Field.getGenericType”来生成新的字段Type。
    需要你了解java.lang.reflect.Type的子类
    java.lang.reflect.GenericArrayType   //通用数组范例 T[]
    java.lang.reflect.ParameterizedType //参数范例,如:  java.util.List    java.util.Map
    java.lang.reflect.TypeVariable  //范例变量 K,V
    java.lang.reflect.WildcardType //通配符类,例如: ?, ? extends Number, ? super Integer
    java.lang.Class  //int.class User.class .....
  JsonString:分析字符串并解析成指定的对应范例
  下面是本JSON框架的分析图

 
下面是简陋版的解析代码。该类没有解析日期,数值等代码,只是方便理解解析过程。
[code]package june.zero.json.reader;import java.io.IOException;import java.io.Reader;import java.io.StringReader;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;public class JsonSimpleParser implements CharSequence {    public static void main(String[] args) {        String json = "[{a:1,b:2},[1,2,3]]";        Object obj = new JsonSimpleParser().parse(json);        System.out.println(obj);    }        protected static final String JSON_EXTRA_CHAR_ERROR = "Extra characters exist! ";    protected static final String JSON_OBJECT_COLON_ERROR = "Wrong format of JsonObject, no separator colon! ";    protected static final String JSON_OBJECT_END_ERROR = "The JsonObject format must end with comma as a separator or with right curly brace! ";    protected static final String JSON_ARRAY_END_ERROR = "The JsonArray format must end with comma as a separator or with right bracket! ";    protected static final String JSON_STRING2_END_ERROR = "The character starts with double quotation marks, but does not end with double quotation marks! ";    protected static final String JSON_STRING1_END_ERROR = "The character starts with single quotation marks, but does not end with single quotation marks! ";        protected Reader reader;    protected static final int capacity = 1024;    protected final char[] cache = new char[capacity];    protected int count;    protected int position;        public void read() throws IOException {        this.position = 0;        this.count = this.reader.read(this.cache,0,capacity);    }        /**     * 当前指针指向的字符     * @return     */    public char current() {        if(this.count==-1) return '\uffff';        return this.cache[this.position];    }    /**     * 当前指针指向的索引下标     * @return     */    public int position() {        return this.position;    }    /**     * 指针指向下一个字符,判断是否需要重新读取数据     * @return     * @throws IOException     */    public boolean nextRead() throws IOException {        return ++this.position>=this.count;    }        /**     * 指针指向下一个字符     * @return     * @throws IOException     */    public void next() throws IOException {        if(++this.position>=this.count){            this.position = 0;            this.count = this.reader.read(this.cache,0,capacity);        }    }    /**     * 跳过空白字符     * @throws IOException     */    public void skip() throws IOException {        while(this.count!=-1&&Character.isWhitespace(this.current())){            if(++this.position>=this.count){                this.position = 0;                this.count = this.reader.read(this.cache,0,capacity);            }        }    }        public Object parse(String json) throws RuntimeException {        return parse(new StringReader(json));    }        /**     * 解析     */    public Object parse(Reader reader) throws RuntimeException {        try {            this.reader = reader;            this.read();            Object value = parse();            this.skip();            if(this.count!=-1){                throw new RuntimeException(JSON_EXTRA_CHAR_ERROR);            }            return value;        } catch (Throwable e) {            throw new RuntimeException(e);        } finally {            try {                this.close();            } catch (IOException e) {                            }        }    }        protected Object parse() throws IOException {        this.skip();        char current = this.current();        if(current=='{'){            this.next();            this.skip();            Map object = new HashMap();            if(this.current() == '}') {                this.next();                return object;            }            do{                Object key = parse();                this.skip();                if(this.current()!=':'){                    throw new RuntimeException(JSON_OBJECT_COLON_ERROR);                }                this.next();                object.put(key, parse());                this.skip();                current = this.current();                if(current=='}') break;                if(current!=','){                    throw new RuntimeException(JSON_OBJECT_END_ERROR);                }                this.next();                this.skip();            }while(true);            this.next();            return object;        }        if(current=='['){            this.next();            this.skip();            List array = new ArrayList();            if(this.current() == ']'){                this.next();                return array;            }            do{                array.add(parse());                this.skip();                current = this.current();                if(current==']') break;                if(current!=','){                    throw new RuntimeException(JSON_ARRAY_END_ERROR);                }                this.next();                this.skip();            }while(true);            this.next();            return array;        }        this.skip();        StringBuilder string = new StringBuilder();        if(current=='"'){            this.next();                        int offset = this.position;            while(this.count!=-1&& this.current()!='"'){                if(this.nextRead()){                    string.append(this, offset, this.position);                    offset = 0;                    this.position = 0;                    this.count = this.reader.read(this.cache,0,capacity);                }            }            string.append(this, offset, this.position);                        if(this.current()!='"'){                throw new RuntimeException(JSON_STRING2_END_ERROR);            }            this.next();                        return string;        }        if(current=='\''){            if(++this.position>=this.count){                this.position = 0;                this.count = this.reader.read(this.cache,0,capacity);            }                        int offset = this.position;            while(this.count!=-1&&this.current()!='\''){                if(this.nextRead()){                    string.append(this, offset, this.position);                    offset = 0;                    this.read();                }            }            string.append(this, offset, this.position);                        if(this.current()!='\''){                throw new RuntimeException(JSON_STRING1_END_ERROR);            }            this.next();                        return string;        }                int offset = this.position;        while(this.count!=-1&&                (current = this.current())!=','&&                current!=':'&&                current!=']'&&                current!='}'&&                !Character.isWhitespace(current)){            if(this.nextRead()){                string.append(this, offset, this.position);                offset = 0;                this.read();            }        }        string.append(this, offset, this.position);        return string;    }        /**     * 关闭流     * @throws IOException     */    public void close() throws IOException{        if(this.reader!=null){            this.reader.close();            this.reader = null;        }    }        @Override    public char charAt(int index) {        return this.cache[index];    }    @Override    public int length() {        return this.count;    }    @Override    public CharSequence subSequence(int start, int end) {        if (start < 0)            throw new StringIndexOutOfBoundsException(start);        if (end > count)            throw new StringIndexOutOfBoundsException(end);        if (start > end)            throw new StringIndexOutOfBoundsException(end - start);        StringBuilder string = new StringBuilder();        for (int i = start; i < end; i++) {            string.append(this.cache);        }        return string;    }    @Override    public String toString() {        if(this.count

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

农妇山泉一亩田

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表