org.json package

  • 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.
    • ```java
      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.

猜你喜欢

转载自www.cnblogs.com/Black-Cobra/p/8862630.html