Package org.kissweb
Class JsonPath
java.lang.Object
org.kissweb.JsonPath
This class provides a variety of features on top of the functionality provided by org.json.
In addition to being able to parse a string into a json object, the json object can also be obtained
from a file. Once the json object is obtained, embedded elements may be indexed similar to XPath thus
providing an easy method of destructuring a complex json object. Lastly, rather than throw an exception
when a data item is missing, these methods return null.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic Boolean
getBoolean
(String delimiter, org.json.JSONObject jobj, String path) Traverse an object returning the Boolean referenced bypath
.static Boolean
getBoolean
(org.json.JSONObject jobj, String path) Traverse an object returning the Boolean referenced bypath
.static Character
getCharacter
(String delimiter, org.json.JSONObject jobj, String path) Traverse an object returning the Character referenced bypath
.static Character
getCharacter
(org.json.JSONObject jobj, String path) Traverse an object returning the Character referenced bypath
.static Integer
Traverse an object returning the Integer date referenced bypath
.static Integer
Traverse an object returning the Integer date referenced bypath
.static Double
Traverse an object returning the Double referenced bypath
.static Double
Traverse an object returning the Double referenced bypath
.static Integer
getInteger
(String delimiter, org.json.JSONObject jobj, String path) Traverse an object returning the Integer referenced bypath
.static Integer
getInteger
(org.json.JSONObject jobj, String path) Traverse an object returning the Integer referenced bypath
.static org.json.JSONArray
getJSONArray
(String delimiter, org.json.JSONObject jobj, String path) Traverse an object returning the JSONArray referenced bypath
.static org.json.JSONArray
getJSONArray
(org.json.JSONObject jobj, String path) Traverse an object returning the JSONArray referenced bypath
.static Long
Traverse an object returning the Long referenced bypath
.static Long
Traverse an object returning the Long referenced bypath
.static org.json.JSONObject
Traverse an object returning the sub-object referenced bypath
.static org.json.JSONObject
Traverse an object returning the sub-object referenced bypath
.static String
Traverse an object returning the String referenced bypath
.static String
Traverse an object returning the String referenced bypath
.static void
static org.json.JSONObject
This takes a JSON string or a file name (containing JSON) and returns a JSON object.
-
Constructor Details
-
JsonPath
public JsonPath()
-
-
Method Details
-
toJson
This takes a JSON string or a file name (containing JSON) and returns a JSON object.
If in a string, the string must start with "{" or space character- Parameters:
str
- the json string or file name containing a json string- Returns:
- Throws:
IOException
-
getObject
public static org.json.JSONObject getObject(String delimiter, org.json.JSONObject jobj, String path) Traverse an object returning the sub-object referenced bypath
.- Parameters:
delimiter
- the delimiter used inpath
to delimit the each path sub-partjobj
- the initial json objectpath
- a string indicating the path to the desired element- Returns:
- the json object being referenced by path or null if not found
-
getObject
Traverse an object returning the sub-object referenced bypath
. The delimiter used inpath
is a period.- Parameters:
jobj
- the initial json objectpath
- a string indicating the path to the desired element- Returns:
- the json object being referenced by path or null if not found
-
getString
Traverse an object returning the String referenced bypath
.- Parameters:
delimiter
- the delimiter used inpath
to delimit the each path sub-partjobj
- the initial json objectpath
- a string indicating the path to the desired element- Returns:
- the String being referenced by path or null if not found
-
getString
Traverse an object returning the String referenced bypath
. The delimiter used inpath
is a period.- Parameters:
jobj
- the initial json objectpath
- a string indicating the path to the desired element- Returns:
- the String being referenced by path or null if not found
-
getInteger
Traverse an object returning the Integer referenced bypath
.- Parameters:
delimiter
- the delimiter used inpath
to delimit the each path sub-partjobj
- the initial json objectpath
- a string indicating the path to the desired element- Returns:
- the Integer being referenced by path or null if not found
-
getInteger
Traverse an object returning the Integer referenced bypath
. The delimiter used inpath
is a period.- Parameters:
jobj
- the initial json objectpath
- a string indicating the path to the desired element- Returns:
- the Integer being referenced by path or null if not found
-
getDouble
Traverse an object returning the Double referenced bypath
.- Parameters:
delimiter
- the delimiter used inpath
to delimit the each path sub-partjobj
- the initial json objectpath
- a string indicating the path to the desired element- Returns:
- the Double being referenced by path or null if not found
-
getDouble
Traverse an object returning the Double referenced bypath
. The delimiter used inpath
is a period.- Parameters:
jobj
- the initial json objectpath
- a string indicating the path to the desired element- Returns:
- the Double being referenced by path or null if not found
-
getJSONArray
public static org.json.JSONArray getJSONArray(String delimiter, org.json.JSONObject jobj, String path) Traverse an object returning the JSONArray referenced bypath
.- Parameters:
delimiter
- the delimiter used inpath
to delimit the each path sub-partjobj
- the initial json objectpath
- a string indicating the path to the desired element- Returns:
- the JSONArray being referenced by path or null if not found
-
getJSONArray
Traverse an object returning the JSONArray referenced bypath
. The delimiter used inpath
is a period.- Parameters:
jobj
- the initial json objectpath
- a string indicating the path to the desired element- Returns:
- the JSONArray being referenced by path or null if not found
-
getBoolean
Traverse an object returning the Boolean referenced bypath
.- Parameters:
delimiter
- the delimiter used inpath
to delimit the each path sub-partjobj
- the initial json objectpath
- a string indicating the path to the desired element- Returns:
- the Boolean being referenced by path or null if not found
-
getBoolean
Traverse an object returning the Boolean referenced bypath
. The delimiter used inpath
is a period.- Parameters:
jobj
- the initial json objectpath
- a string indicating the path to the desired element- Returns:
- the Boolean being referenced by path or null if not found
-
getLong
Traverse an object returning the Long referenced bypath
.- Parameters:
delimiter
- the delimiter used inpath
to delimit the each path sub-partjobj
- the initial json objectpath
- a string indicating the path to the desired element- Returns:
- the Long being referenced by path or null if not found
-
getLong
Traverse an object returning the Long referenced bypath
. The delimiter used inpath
is a period.- Parameters:
jobj
- the initial json objectpath
- a string indicating the path to the desired element- Returns:
- the Long being referenced by path or null if not found
-
getCharacter
Traverse an object returning the Character referenced bypath
.
If the field contains mor than one character, the first is returned.- Parameters:
delimiter
- the delimiter used inpath
to delimit the each path sub-partjobj
- the initial json objectpath
- a string indicating the path to the desired element- Returns:
- the Character being referenced by path or null if not found
-
getCharacter
Traverse an object returning the Character referenced bypath
. The delimiter used inpath
is a period.
If the field contains mor than one character, the first is returned.- Parameters:
jobj
- the initial json objectpath
- a string indicating the path to the desired element- Returns:
- the Character being referenced by path or null if not found
-
getDate
Traverse an object returning the Integer date referenced bypath
.
The Integer date returned is of the form YYYYMMDD. So, 6/8/2018 would be returned as 20180608. A null is returned if the data cannot be obtained.- Parameters:
delimiter
- the delimiter used inpath
to delimit the each path sub-partjobj
- the initial json objectpath
- a string indicating the path to the desired element- Returns:
- the Integer date being referenced by path or null if not found
-
getDate
Traverse an object returning the Integer date referenced bypath
. The delimiter used inpath
is a period.
The Integer date returned is of the form YYYYMMDD. So, 6/8/2018 would be returned as 20180608. A null is returned if the data cannot be obtained.- Parameters:
jobj
- the initial json objectpath
- a string indicating the path to the desired element- Returns:
- the Integer date being referenced by path or null if not found
-
main
- Throws:
IOException
-