生如夏花2017

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
  • JSON is a light-weight,language independent,data interchange format.
  • org.json package implement JSON encoders/decoders in Java.It also includes the cpability to convert between JSON and XML,HTTP headers,Cookies and CDL.
  • This is a reference implementation.
  • The package compiles on Java 1.6-1.8
  • JSONObject.java:The JSONObject can parse text from a String or a JSONTokener to produce a map-like object.The object provides methods for manipulating its contents,and for producing a JSON compliant object serialization.
    •   public JSONObject();
        public JSONObject(JSONObject jo, String[] names);
        public JSONObject(JSONTokener x) throws JSONException ;
        public JSONObject(Map<?, ?> m);
        public JSONObject(Object bean);
        public JSONObject(Object object, String names[]);
        public JSONObject(String source) throws JSONException;
        public JSONObject(String baseName, Locale locale) throws JSONException;
      
  • JSONArray.java:The JSONArray can parse text from a String or a JSONTokener to produce a vector-like object.The object provides methods for manipulating its contents,and for producing a JSON compliant array serialization.
    •   public JSONArray() {
            this.myArrayList = new ArrayList<Object>();
        }
        public JSONArray(JSONTokener x) throws JSONException;
        public JSONArray(String source) throws JSONException;
        public JSONArray(Collection<?> collection);
        public JSONArray(Object array) throws JSONException;
      
  • JSONTokener.java:The JSONTokener breaks a text into a sequence of individual tokens.It can be constructed from String,Reader,or InputStream.
    •   public JSONTokener(Reader reader);
        public JSONTokener(InputStream inputStream);
        public JSONTokener(String s);
      
  • JSONException.java: The JSONException is the standard exception type thrown by this package.
    •   public class JSONException extends RuntimeException{
            public JSONException(final String message) {
                super(message);
            }
            public JSONException(final String message, final Throwable cause) {
                super(message, cause);
            }
             public JSONException(final Throwable cause) {
                super(cause.getMessage(), cause);
             }
        }
      
  • Cookie.java: Cookie provides support for converting between JSON and cookies.
    •   public static JSONObject toJSONObject(String string) throws JSONException {
        String         name;
        JSONObject     jo = new JSONObject();
        Object         value;
        JSONTokener x = new JSONTokener(string);
        jo.put("name", x.nextTo('='));
        x.next('=');
        jo.put("value", x.nextTo(';'));
        x.next();
        while (x.more()) {
            name = unescape(x.nextTo("=;"));
            if (x.next() != '=') {
                if (name.equals("secure")) {
                    value = Boolean.TRUE;
                } else {
                    throw x.syntaxError("Missing '=' in cookie parameter.");
                }
            } else {
                value = unescape(x.nextTo(';'));
                x.next();
            }
            jo.put(name, value);
        }
        return jo;
      
    }
  • CookieList.java: CookieList provides support for converting between JSON and cookie lists.
    •   public static JSONObject toJSONObject(String string) throws JSONException {
        JSONObject jo = new JSONObject();
        JSONTokener x = new JSONTokener(string);
        while (x.more()) {
            String name = Cookie.unescape(x.nextTo('='));
            x.next('=');
            jo.put(name, Cookie.unescape(x.nextTo(';')));
            x.next();
        }
        return jo;
      
    }
  • HTTP.java: HTTP provides support for converting between JSON and HTTP headers.
    •   public static JSONObject toJSONObject(String string) throws JSONException {
        JSONObject     jo = new JSONObject();
        HTTPTokener    x = new HTTPTokener(string);
        String         token;
      
        token = x.nextToken();
        if (token.toUpperCase(Locale.ROOT).startsWith("HTTP")) {
      
        // Response
      
                    jo.put("HTTP-Version", token);
                    jo.put("Status-Code", x.nextToken());
                    jo.put("Reason-Phrase", x.nextTo('\0'));
                    x.next();
      
                } else {
      
        // Request
      
                    jo.put("Method", token);
                    jo.put("Request-URI", x.nextToken());
                    jo.put("HTTP-Version", x.nextToken());
                }
      
        // Fields
      
                while (x.more()) {
                    String name = x.nextTo(':');
                    x.next(':');
                    jo.put(name, x.nextTo('\0'));
                    x.next();
                }
                return jo;
        }
      
  • XML.java: XML provides support for converting between JSON and XML.
posted on 2018-04-17 11:29  生如夏花2017  阅读(128)  评论(0编辑  收藏  举报