Class JSONTokener


  • public class JSONTokener
    extends java.lang.Object
    Parses a JSON (RFC 4627) encoded string into the corresponding object. Most clients of this class will use only need the constructor and nextValue() method. Example usage:
     String json = "{"
             + "  \"query\": \"Pizza\", "
             + "  \"locations\": [ 94043, 90210 ] "
             + "}";
    
     JSONObject object = (JSONObject) new JSONTokener(json).nextValue();
     String query = object.getString("query");
     JSONArray locations = object.getJSONArray("locations");

    For best interoperability and performance use JSON that complies with RFC 4627, such as that generated by JSONStringer. For legacy reasons this parser is lenient, so a successful parse does not indicate that the input string was valid JSON. All the following syntax errors will be ignored:

    • End of line comments starting with // or # and ending with a newline character.
    • C-style comments starting with /* and ending with */. Such comments may not be nested.
    • Strings that are unquoted or 'single quoted'.
    • Hexadecimal integers prefixed with 0x or 0X.
    • Octal integers prefixed with 0.
    • Array elements separated by ;.
    • Unnecessary array separators. These are interpreted as if null was the omitted value.
    • Key-value pairs separated by = or =>.
    • Key-value pairs separated by ;.

    Each tokener may be used to parse a single JSON string. Instances of this class are not thread safe. Although this class is nonfinal, it was not designed for inheritance and should not be subclassed. In particular, self-use by overrideable methods is not specified. See Effective Java Item 17, "Design and Document or inheritance or else prohibit it" for further information.

    • Constructor Summary

      Constructors 
      Constructor Description
      JSONTokener​(java.lang.String in)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void back()  
      static int dehexchar​(char hex)  
      boolean more()  
      char next()  
      char next​(char c)  
      java.lang.String next​(int length)  
      char nextClean()  
      java.lang.String nextString​(char quote)
      Returns the string up to but not including quote, unescaping any character escape sequences encountered along the way.
      java.lang.String nextTo​(char excluded)  
      java.lang.String nextTo​(java.lang.String excluded)  
      java.lang.Object nextValue()
      Returns the next value from the input.
      void skipPast​(java.lang.String thru)  
      char skipTo​(char to)  
      java.lang.String toString()
      Returns the current position and the entire input string.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • JSONTokener

        public JSONTokener​(java.lang.String in)
        Parameters:
        in - JSON encoded string. Null is not permitted and will yield a tokener that throws NullPointerExceptions when methods are called.
    • Method Detail

      • nextString

        public java.lang.String nextString​(char quote)
                                    throws JSONException
        Returns the string up to but not including quote, unescaping any character escape sequences encountered along the way. The opening quote should have already been read. This consumes the closing quote, but does not include it in the returned string.
        Parameters:
        quote - either ' or ".
        Returns:
        the string up to but not including quote
        Throws:
        java.lang.NumberFormatException - if any unicode escape sequences are malformed.
        JSONException - if processing of json failed
      • toString

        public java.lang.String toString()
        Returns the current position and the entire input string.
        Overrides:
        toString in class java.lang.Object
        Returns:
        the current position and the entire input string.
      • more

        public boolean more()
      • next

        public char next()
      • nextTo

        public java.lang.String nextTo​(java.lang.String excluded)
      • nextTo

        public java.lang.String nextTo​(char excluded)
      • skipPast

        public void skipPast​(java.lang.String thru)
      • skipTo

        public char skipTo​(char to)
      • back

        public void back()
      • dehexchar

        public static int dehexchar​(char hex)