Class JSONObjectUtils


  • public final class JSONObjectUtils
    extends Object
    JSON object helper methods for parsing and typed retrieval of member values.
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static boolean containsKey​(net.minidev.json.JSONObject jsonObject, String key)
      Returns true if the JSON object is defined and contains the specified key.
      static boolean getBoolean​(net.minidev.json.JSONObject o, String key)
      Gets a boolean member of a JSON object.
      static boolean getBoolean​(net.minidev.json.JSONObject o, String key, boolean def)
      Gets a boolean member of a JSON object.
      static double getDouble​(net.minidev.json.JSONObject o, String key)
      Gets a number member of a JSON object as double.
      static double getDouble​(net.minidev.json.JSONObject o, String key, double def)
      Gets a number member of a JSON object as double.
      static javax.mail.internet.InternetAddress getEmail​(net.minidev.json.JSONObject o, String key)
      Deprecated.
      static <T extends Enum<T>>
      T
      getEnum​(net.minidev.json.JSONObject o, String key, Class<T> enumClass)
      Gets a string member of a JSON object as an enumerated object.
      static <T extends Enum<T>>
      T
      getEnum​(net.minidev.json.JSONObject o, String key, Class<T> enumClass, T def)
      Gets a string member of a JSON object as an enumerated object.
      static float getFloat​(net.minidev.json.JSONObject o, String key)
      Gets a number member of a JSON object float.
      static float getFloat​(net.minidev.json.JSONObject o, String key, float def)
      Gets a number member of a JSON object float.
      static <T> T getGeneric​(net.minidev.json.JSONObject o, String key, Class<T> clazz)
      Gets a generic member of a JSON object.
      static int getInt​(net.minidev.json.JSONObject o, String key)
      Gets an number member of a JSON object as int.
      static int getInt​(net.minidev.json.JSONObject o, String key, int def)
      Gets an number member of a JSON object as int.
      static net.minidev.json.JSONArray getJSONArray​(net.minidev.json.JSONObject o, String key)
      Gets a JSON array member of a JSON object.
      static net.minidev.json.JSONArray getJSONArray​(net.minidev.json.JSONObject o, String key, net.minidev.json.JSONArray def)
      Gets a JSON array member of a JSON object.
      static net.minidev.json.JSONObject getJSONObject​(net.minidev.json.JSONObject o, String key)
      Gets a JSON object member of a JSON object.
      static net.minidev.json.JSONObject getJSONObject​(net.minidev.json.JSONObject o, String key, net.minidev.json.JSONObject def)
      Gets a JSON object member of a JSON object.
      static List<Object> getList​(net.minidev.json.JSONObject o, String key)
      Gets a list member of a JSON object.
      static List<Object> getList​(net.minidev.json.JSONObject o, String key, List<Object> def)
      Gets a list member of a JSON object.
      static long getLong​(net.minidev.json.JSONObject o, String key)
      Gets a number member of a JSON object as long.
      static long getLong​(net.minidev.json.JSONObject o, String key, long def)
      Gets a number member of a JSON object as long.
      static Number getNumber​(net.minidev.json.JSONObject o, String key)
      Gets a number member of a JSON object as java.lang.Number.
      static Number getNumber​(net.minidev.json.JSONObject o, String key, Number def)
      Gets a number member of a JSON object as java.lang.Number.
      static String getString​(net.minidev.json.JSONObject o, String key)
      Gets a string member of a JSON object.
      static String getString​(net.minidev.json.JSONObject o, String key, String def)
      Gets a string member of a JSON object.
      static String[] getStringArray​(net.minidev.json.JSONObject o, String key)
      Gets a string array member of a JSON object.
      static String[] getStringArray​(net.minidev.json.JSONObject o, String key, String[] def)
      Gets a string array member of a JSON object.
      static List<String> getStringList​(net.minidev.json.JSONObject o, String key)
      Gets a string list member of a JSON object.
      static List<String> getStringList​(net.minidev.json.JSONObject o, String key, List<String> def)
      Gets a string list member of a JSON object.
      static Set<String> getStringSet​(net.minidev.json.JSONObject o, String key)
      Gets a string array member of a JSON object as a string set.
      static Set<String> getStringSet​(net.minidev.json.JSONObject o, String key, Set<String> def)
      Gets a string array member of a JSON object as a string set.
      static URI getURI​(net.minidev.json.JSONObject o, String key)
      Gets a string member of a JSON object as java.net.URI.
      static URI getURI​(net.minidev.json.JSONObject o, String key, URI def)
      Gets a string member of a JSON object as java.net.URI.
      static URL getURL​(net.minidev.json.JSONObject o, String key)
      Gets a string member of a JSON object as java.net.URL.
      static net.minidev.json.JSONObject parse​(String s)
      Parses a JSON object.
      static net.minidev.json.JSONObject parseJSONObject​(String s)
      Deprecated.
    • Method Detail

      • containsKey

        public static boolean containsKey​(net.minidev.json.JSONObject jsonObject,
                                          String key)
        Returns true if the JSON object is defined and contains the specified key.
        Parameters:
        jsonObject - The JSON object to check. May be null.
        key - The key to check. Must not be null.
        Returns:
        true if the JSON object is defined and contains the specified key, else false.
      • parse

        public static net.minidev.json.JSONObject parse​(String s)
                                                 throws ParseException
        Parses a JSON object.

        Specific JSON to Java entity mapping (as per JSON Simple):

        • JSON numbers mapped to java.lang.Number.
        • JSON integer numbers mapped to long.
        • JSON fraction numbers mapped to double.
        Parameters:
        s - The JSON object string to parse. Must not be null.
        Returns:
        The JSON object.
        Throws:
        ParseException - If the string cannot be parsed to a JSON object.
      • getGeneric

        public static <T> T getGeneric​(net.minidev.json.JSONObject o,
                                       String key,
                                       Class<T> clazz)
                                throws ParseException
        Gets a generic member of a JSON object.
        Parameters:
        o - The JSON object. Must not be null.
        key - The JSON object member key. Must not be null.
        clazz - The expected class of the JSON object member value. Must not be null.
        Returns:
        The JSON object member value.
        Throws:
        ParseException - If the value is missing, null or not of the expected type.
      • getBoolean

        public static boolean getBoolean​(net.minidev.json.JSONObject o,
                                         String key)
                                  throws ParseException
        Gets a boolean member of a JSON object.
        Parameters:
        o - The JSON object. Must not be null.
        key - The JSON object member key. Must not be null.
        Returns:
        The member value.
        Throws:
        ParseException - If the value is missing, null or not of the expected type.
      • getBoolean

        public static boolean getBoolean​(net.minidev.json.JSONObject o,
                                         String key,
                                         boolean def)
                                  throws ParseException
        Gets a boolean member of a JSON object.
        Parameters:
        o - The JSON object. Must not be null.
        key - The JSON object member key. Must not be null.
        def - The default value to return if the key is not present or. the value is null. May be null.
        Returns:
        The member value.
        Throws:
        ParseException - If the value is not of the expected type.
      • getInt

        public static int getInt​(net.minidev.json.JSONObject o,
                                 String key)
                          throws ParseException
        Gets an number member of a JSON object as int.
        Parameters:
        o - The JSON object. Must not be null.
        key - The JSON object member key. Must not be null.
        Returns:
        The member value.
        Throws:
        ParseException - If the value is missing, null or not of the expected type.
      • getInt

        public static int getInt​(net.minidev.json.JSONObject o,
                                 String key,
                                 int def)
                          throws ParseException
        Gets an number member of a JSON object as int.
        Parameters:
        o - The JSON object. Must not be null.
        key - The JSON object member key. Must not be null.
        def - The default value to return if the key is not present or the value is null.
        Returns:
        The member value.
        Throws:
        ParseException - If the value is not of the expected type.
      • getLong

        public static long getLong​(net.minidev.json.JSONObject o,
                                   String key)
                            throws ParseException
        Gets a number member of a JSON object as long.
        Parameters:
        o - The JSON object. Must not be null.
        key - The JSON object member key. Must not be null.
        Returns:
        The member value.
        Throws:
        ParseException - If the value is missing, null or not of the expected type.
      • getLong

        public static long getLong​(net.minidev.json.JSONObject o,
                                   String key,
                                   long def)
                            throws ParseException
        Gets a number member of a JSON object as long.
        Parameters:
        o - The JSON object. Must not be null.
        key - The JSON object member key. Must not be null.
        def - The default value to return if the key is not present or the value is null.
        Returns:
        The member value.
        Throws:
        ParseException - If the value is not of the expected type.
      • getFloat

        public static float getFloat​(net.minidev.json.JSONObject o,
                                     String key)
                              throws ParseException
        Gets a number member of a JSON object float.
        Parameters:
        o - The JSON object. Must not be null.
        key - The JSON object member key. Must not be null.
        Returns:
        The member value.
        Throws:
        ParseException - If the value is missing, null or not of the expected type.
      • getFloat

        public static float getFloat​(net.minidev.json.JSONObject o,
                                     String key,
                                     float def)
                              throws ParseException
        Gets a number member of a JSON object float.
        Parameters:
        o - The JSON object. Must not be null.
        key - The JSON object member key. Must not be null.
        def - The default value to return if the key is not present or the value is null.
        Returns:
        The member value.
        Throws:
        ParseException - If the value is not of the expected type.
      • getDouble

        public static double getDouble​(net.minidev.json.JSONObject o,
                                       String key)
                                throws ParseException
        Gets a number member of a JSON object as double.
        Parameters:
        o - The JSON object. Must not be null.
        key - The JSON object member key. Must not be null.
        Returns:
        The member value.
        Throws:
        ParseException - If the value is missing, null or not of the expected type.
      • getDouble

        public static double getDouble​(net.minidev.json.JSONObject o,
                                       String key,
                                       double def)
                                throws ParseException
        Gets a number member of a JSON object as double.
        Parameters:
        o - The JSON object. Must not be null.
        key - The JSON object member key. Must not be null.
        def - The default value to return if the key is not present or the value is null.
        Returns:
        The member value.
        Throws:
        ParseException - If the value is not of the expected type.
      • getNumber

        public static Number getNumber​(net.minidev.json.JSONObject o,
                                       String key)
                                throws ParseException
        Gets a number member of a JSON object as java.lang.Number.
        Parameters:
        o - The JSON object. Must not be null.
        key - The JSON object member key. Must not be null.
        Returns:
        The member value.
        Throws:
        ParseException - If the value is missing, null or not of the expected type.
      • getNumber

        public static Number getNumber​(net.minidev.json.JSONObject o,
                                       String key,
                                       Number def)
                                throws ParseException
        Gets a number member of a JSON object as java.lang.Number.
        Parameters:
        o - The JSON object. Must not be null.
        key - The JSON object member key. Must not be null.
        def - The default value to return if the key is not present or the value is null. May be null.
        Returns:
        The member value.
        Throws:
        ParseException - If the value is not of the expected type.
      • getString

        public static String getString​(net.minidev.json.JSONObject o,
                                       String key)
                                throws ParseException
        Gets a string member of a JSON object.
        Parameters:
        o - The JSON object. Must not be null.
        key - The JSON object member key. Must not be null.
        Returns:
        The member value.
        Throws:
        ParseException - If the value is missing, null or not of the expected type.
      • getString

        public static String getString​(net.minidev.json.JSONObject o,
                                       String key,
                                       String def)
                                throws ParseException
        Gets a string member of a JSON object.
        Parameters:
        o - The JSON object. Must not be null.
        key - The JSON object member key. Must not be null.
        def - The default value to return if the key is not present or the value is null. May be null.
        Returns:
        The member value.
        Throws:
        ParseException - If the value is not of the expected type.
      • getEnum

        public static <T extends Enum<T>> T getEnum​(net.minidev.json.JSONObject o,
                                                    String key,
                                                    Class<T> enumClass)
                                             throws ParseException
        Gets a string member of a JSON object as an enumerated object.
        Parameters:
        o - The JSON object. Must not be null.
        key - The JSON object member key. Must not be null.
        enumClass - The enumeration class. Must not be null.
        Returns:
        The member value.
        Throws:
        ParseException - If the value is missing, null or not of the expected type.
      • getEnum

        public static <T extends Enum<T>> T getEnum​(net.minidev.json.JSONObject o,
                                                    String key,
                                                    Class<T> enumClass,
                                                    T def)
                                             throws ParseException
        Gets a string member of a JSON object as an enumerated object.
        Parameters:
        o - The JSON object. Must not be null.
        key - The JSON object member key. Must not be null.
        enumClass - The enumeration class. Must not be null.
        def - The default value to return if the key is not present or the value is null. May be null.
        Returns:
        The member value.
        Throws:
        ParseException - If the value is not of the expected type.
      • getURI

        public static URI getURI​(net.minidev.json.JSONObject o,
                                 String key)
                          throws ParseException
        Gets a string member of a JSON object as java.net.URI.
        Parameters:
        o - The JSON object. Must not be null.
        key - The JSON object member key. Must not be null.
        Returns:
        The member value.
        Throws:
        ParseException - If the value is missing, null or not of the expected type.
      • getURI

        public static URI getURI​(net.minidev.json.JSONObject o,
                                 String key,
                                 URI def)
                          throws ParseException
        Gets a string member of a JSON object as java.net.URI.
        Parameters:
        o - The JSON object. Must not be null.
        key - The JSON object member key. Must not be null.
        def - The default value to return if the key is not present or the value is null. May be null.
        Returns:
        The member value.
        Throws:
        ParseException - If the value is not of the expected type.
      • getURL

        public static URL getURL​(net.minidev.json.JSONObject o,
                                 String key)
                          throws ParseException
        Gets a string member of a JSON object as java.net.URL.
        Parameters:
        o - The JSON object. Must not be null.
        key - The JSON object member key. Must not be null.
        Returns:
        The member value.
        Throws:
        ParseException - If the value is missing, null or not of the expected type.
      • getEmail

        @Deprecated
        public static javax.mail.internet.InternetAddress getEmail​(net.minidev.json.JSONObject o,
                                                                   String key)
                                                            throws ParseException
        Deprecated.
        Gets a string member of a JSON object as javax.mail.internet.InternetAddress.
        Parameters:
        o - The JSON object. Must not be null.
        key - The JSON object member key. Must not be null.
        Returns:
        The member value.
        Throws:
        ParseException - If the value is missing, null or not of the expected type.
      • getJSONArray

        public static net.minidev.json.JSONArray getJSONArray​(net.minidev.json.JSONObject o,
                                                              String key)
                                                       throws ParseException
        Gets a JSON array member of a JSON object.
        Parameters:
        o - The JSON object. Must not be null.
        key - The JSON object member key. Must not be null.
        Returns:
        The member value.
        Throws:
        ParseException - If the value is missing, null or not of the expected type.
      • getJSONArray

        public static net.minidev.json.JSONArray getJSONArray​(net.minidev.json.JSONObject o,
                                                              String key,
                                                              net.minidev.json.JSONArray def)
                                                       throws ParseException
        Gets a JSON array member of a JSON object.
        Parameters:
        o - The JSON object. Must not be null.
        key - The JSON object member key. Must not be null.
        def - The default value to return if the key is not present or the value is null. May be null.
        Returns:
        The member value.
        Throws:
        ParseException - If the value is not of the expected type.
      • getList

        public static List<ObjectgetList​(net.minidev.json.JSONObject o,
                                           String key)
                                    throws ParseException
        Gets a list member of a JSON object.
        Parameters:
        o - The JSON object. Must not be null.
        key - The JSON object member key. Must not be null.
        Returns:
        The member value.
        Throws:
        ParseException - If the value is missing, null or not of the expected type.
      • getList

        public static List<ObjectgetList​(net.minidev.json.JSONObject o,
                                           String key,
                                           List<Object> def)
                                    throws ParseException
        Gets a list member of a JSON object.
        Parameters:
        o - The JSON object. Must not be null.
        key - The JSON object member key. Must not be null.
        def - The default value to return if the key is not present or the value is null. May be null.
        Returns:
        The member value.
        Throws:
        ParseException - If the value is not of the expected type.
      • getStringArray

        public static String[] getStringArray​(net.minidev.json.JSONObject o,
                                              String key)
                                       throws ParseException
        Gets a string array member of a JSON object.
        Parameters:
        o - The JSON object. Must not be null.
        key - The JSON object member key. Must not be null.
        Returns:
        The member value.
        Throws:
        ParseException - If the value is missing, null or not of the expected type.
      • getStringArray

        public static String[] getStringArray​(net.minidev.json.JSONObject o,
                                              String key,
                                              String[] def)
                                       throws ParseException
        Gets a string array member of a JSON object.
        Parameters:
        o - The JSON object. Must not be null.
        key - The JSON object member key. Must not be null.
        def - The default value to return if the key is not present or the value is null. May be null.
        Returns:
        The member value.
        Throws:
        ParseException - If the value is not of the expected type.
      • getStringList

        public static List<StringgetStringList​(net.minidev.json.JSONObject o,
                                                 String key)
                                          throws ParseException
        Gets a string list member of a JSON object.
        Parameters:
        o - The JSON object. Must not be null.
        key - The JSON object member key. Must not be null.
        Returns:
        The member value.
        Throws:
        ParseException - If the value is missing, null or not of the expected type.
      • getStringList

        public static List<StringgetStringList​(net.minidev.json.JSONObject o,
                                                 String key,
                                                 List<String> def)
                                          throws ParseException
        Gets a string list member of a JSON object.
        Parameters:
        o - The JSON object. Must not be null.
        key - The JSON object member key. Must not be null.
        def - The default value to return if the key is not present or the value is null. May be null.
        Returns:
        The member value.
        Throws:
        ParseException - If the value is not of the expected type.
      • getStringSet

        public static Set<StringgetStringSet​(net.minidev.json.JSONObject o,
                                               String key)
                                        throws ParseException
        Gets a string array member of a JSON object as a string set.
        Parameters:
        o - The JSON object. Must not be null.
        key - The JSON object member key. Must not be null.
        Returns:
        The member value.
        Throws:
        ParseException - If the value is missing, null or not of the expected type.
      • getStringSet

        public static Set<StringgetStringSet​(net.minidev.json.JSONObject o,
                                               String key,
                                               Set<String> def)
                                        throws ParseException
        Gets a string array member of a JSON object as a string set.
        Parameters:
        o - The JSON object. Must not be null.
        key - The JSON object member key. Must not be null.
        def - The default value to return if the key is not present or the value is null. May be null.
        Returns:
        The member value.
        Throws:
        ParseException - If the value is not of the expected type.
      • getJSONObject

        public static net.minidev.json.JSONObject getJSONObject​(net.minidev.json.JSONObject o,
                                                                String key)
                                                         throws ParseException
        Gets a JSON object member of a JSON object.
        Parameters:
        o - The JSON object. Must not be null.
        key - The JSON object member key. Must not be null.
        Returns:
        The member value.
        Throws:
        ParseException - If the value is missing, null or not of the expected type.
      • getJSONObject

        public static net.minidev.json.JSONObject getJSONObject​(net.minidev.json.JSONObject o,
                                                                String key,
                                                                net.minidev.json.JSONObject def)
                                                         throws ParseException
        Gets a JSON object member of a JSON object.
        Parameters:
        o - The JSON object. Must not be null.
        key - The JSON object member key. Must not be null.
        def - The default value to return if the key is not present or the value is null. May be null.
        Returns:
        The member value.
        Throws:
        ParseException - If the value is not of the expected type.