public class Result extends Object
JSONParser
and XMLParser
classes. This
expression language allows applications to extract information from
structured data returned by web services with minimal effort. You can read more about it here
.
The expression language works a lot like a very small subset of XPath - the
expression syntax uses the / character for sub-elements and square brackets
for arrays.
Some sample expressions:
Simple expression, get the title of the first photo element.
/photos/photo[1]/title
Globally find the first name of a person with a last name of 'Coolman'.
//person[lastname='Coolman']/firstName
Get the latitude value of the second last result element.
/results[last()-1]/geometry/bounds/northeast/lat
Get the names of players from Germany
/tournament/player[@nationality='Germany']/name
Get the purchase order numbers of any order with a lineitem worth over $5
//order/lineitem[price > 5]/../@ponum
etc
Modifier and Type | Field and Description |
---|---|
static char |
ARRAY_END |
static char |
ARRAY_START |
static String |
JSON |
static char |
SEPARATOR |
static String |
XML |
Modifier and Type | Method and Description |
---|---|
boolean |
equals(Object other)
Indicates whether some other object is "equal to" this one.
|
static Result |
fromContent(Element content)
Create an evaluator object from a parsed XML DOM.
|
static Result |
fromContent(InputStream content,
String format)
Create an evaluator object from a structured content document (XML, JSON,
etc) input stream.
|
static Result |
fromContent(Map content)
Create an evaluator object from parsed JSON content DOM.
|
static Result |
fromContent(Reader content,
String format)
Create an evaluator object from a structured content document (XML, JSON,
etc) input stream.
|
static Result |
fromContent(String content,
String format)
Create an evaluator object from a structured content document (XML, JSON,
etc) as a string.
|
Object |
get(String path)
Get the object value from the requested path.
|
List |
getAsArray(String path)
Get a List of values from the requested path.
|
boolean |
getAsBoolean(String path)
Get a boolean value from the requested path.
|
boolean[] |
getAsBooleanArray(String path)
Get an array of values from the requested path.
|
double |
getAsDouble(String path)
Get a double value from the requested path.
|
double[] |
getAsDoubleArray(String path)
Get an array of values from the requested path.
|
int |
getAsInteger(String path)
Get an integer value from the requested path.
|
int[] |
getAsIntegerArray(String path)
Get an array of values from the requested path.
|
long |
getAsLong(String path)
Get a long value from the requested path.
|
long[] |
getAsLongArray(String path)
Get an array of values from the requested path.
|
String |
getAsString(String path)
Get a string value from the requested path.
|
String[] |
getAsStringArray(String path)
Get an array of string values from the requested path.
|
int |
getSizeOfArray(String path)
Get the size of an array at the requested path.
|
int |
hashCode()
Returns a hashcode value for the object.
|
void |
mapNamespaceAlias(String namespaceURI,
String alias) |
String |
toString()
Convert the object to a formatted structured content document.
|
public static final String JSON
public static final String XML
public static final char SEPARATOR
public static final char ARRAY_START
public static final char ARRAY_END
public static Result fromContent(String content, String format) throws IllegalArgumentException
content
- structured content document as a string.format
- an identifier for the type of content passed (ie. xml,
json, etc).IllegalArgumentException
- thrown if null content or format is
passed.public static Result fromContent(InputStream content, String format) throws IllegalArgumentException, IOException
ConnectionRequest request = new ConnectionRequest() { protected void readResponse(InputStream input) throws IOException { Result evaluator = Result.fromContent(input, Result.JSON); // ... evaluate the result here } // ... etc };
content
- structured content document as a string.format
- an identifier for the type of content passed (ie. xml,
json, etc).IllegalArgumentException
- thrown if null content or format is
passed.IOException
public static Result fromContent(Reader content, String format) throws IllegalArgumentException, IOException
ConnectionRequest request = new ConnectionRequest() { protected void readResponse(InputStream input) throws IOException { Result evaluator = Result.fromContent(input, Result.JSON); // ... evaluate the result here } // ... etc };
content
- structured content document as a string.format
- an identifier for the type of content passed (ie. xml,
json, etc).IllegalArgumentException
- thrown if null content or format is
passed.IOException
public static Result fromContent(Element content) throws IllegalArgumentException
content
- a parsed XML DOM.IllegalArgumentException
- thrown if null content is passed.public static Result fromContent(Map content) throws IllegalArgumentException
content
- JSON content input streamIllegalArgumentException
public int hashCode()
hashCode
in class Object
Object.hashCode()
public boolean equals(Object other)
equals
in class Object
Object.equals(Object)
public String toString()
public boolean getAsBoolean(String path) throws IllegalArgumentException
{ "settings" : [ { "toggle" : "true", ... etc }Expression
boolean value = result.getAsBoolean("/settings[0]/toggle");
path
- Path expression to evaluateIllegalArgumentException
- on error traversing the document, ie.
traversing into an array without using subscripts.public int getAsInteger(String path) throws IllegalArgumentException
{ "settings" { "connection" { "max_retries" : "20", ... etc } }Expression
int value = result.getAsInteger("//connection/max_retries");
path
- Path expression to evaluateIllegalException
- on error traversing the document, ie. traversing
into an array without using subscripts.IllegalArgumentException
public long getAsLong(String path) throws IllegalArgumentException
{ "settings" { "connection" { "timeout_milliseconds" : "100000", ... etc } }Expression
long value = result.getAsLong("/settings/connection/timeout_milliseconds");
path
- Path expression to evaluateIllegalArgumentException
- on error traversing the document, ie.
traversing into an array without using subscripts.public double getAsDouble(String path) throws IllegalArgumentException
{ "geometry" : { "bounds" : { "northeast" : { "lat" : 42.94959820, "lng" : -81.24873959999999 }, "southwest" : { "lat" : 42.94830, "lng" : -81.24901740000001 } }, "location" : { "lat" : 42.94886990, "lng" : -81.24876030 }, "location_type" : "RANGE_INTERPOLATED", "viewport" : { "northeast" : { "lat" : 42.95029808029150, "lng" : -81.24752951970851 }, "southwest" : { "lat" : 42.94760011970850, "lng" : -81.25022748029151 } } // etcExpression
double neBoundsLat = result.getAsDouble("//bounds/northeast/lat"); double neBoundsLong = result.getAsDouble("//bounds/northeast/lng"); double swBoundsLat = result.getAsDouble("//bounds/southwest/lat"); double swBoundsLong = result.getAsDouble("//bounds/southwest/lng"); double memberDiscount = result.getAsDouble("pricing.members.members");
path
- Path expression to evaluateIllegalArgumentException
- on error traversing the document, ie.
traversing into an array without using subscripts.public String getAsString(String path) throws IllegalArgumentException
{ "profile" { "location" { "city" : "London", "region" : "Ontario", "country" : "Canada", ... etc }, }Expression
String city = result.getAsDouble("//city"); String province = result.getAsDouble("//location//region"); String country = result.getAsDouble("profile//location//country");
path
- Path expression to evaluateIllegalArgumentException
- on error traversing the document, ie.
traversing into an array without using subscripts.public Object get(String path) throws IllegalArgumentException
path
- IllegalArgumentException
public int getSizeOfArray(String path) throws IllegalArgumentException
{ "results" : [ { "address_components" : [ { "long_name" : "921-989", "short_name" : "921-989", "types" : [ "street_number" ] }, { "long_name" : "Country Club Crescent", "short_name" : "Country Club Crescent", "types" : [ "route" ] }, { "long_name" : "Ontario", "short_name" : "ON", "types" : [ "administrative_area_level_1", "political" ] }, ... etc } }Expression
int size = result.getSizeOfArray("/results[0]/address_components"); int size2 = result.getSizeOfArray("results"); int size3 = result.getSizeOfArray("/results[0]/address_components[2]/types");
path
- Path expression to evaluateIllegalArgumentException
- on error traversing the document, ie.
traversing into an array without using subscripts.public String[] getAsStringArray(String path) throws IllegalArgumentException
{ "results" : [ { "address_components" : [ { "long_name" : "921-989", "short_name" : "921-989", "types" : [ "street_number" ] }, { "long_name" : "Country Club Crescent", "short_name" : "Country Club Crescent", "types" : [ "route" ] }, { "long_name" : "Ontario", "short_name" : "ON", "types" : [ "administrative_area_level_1", "political" ] }, ... etc } }Expression
String types[] = result .getAsStringArray("/results[0]/address_components[2]/types");
path
- Path expression to evaluateIllegalArgumentException
- on error traversing the document, ie.
traversing into an array without using subscripts.public int[] getAsIntegerArray(String path) throws IllegalArgumentException
{ "results" : [ { "address_components" : [ { "long_name" : "921-989", "short_name" : "921-989", "types" : [ "street_number" ] }, { "long_name" : "Country Club Crescent", "short_name" : "Country Club Crescent", "types" : [ "route" ] }, { "long_name" : "Ontario", "short_name" : "ON", "types" : [ "administrative_area_level_1", "political" ] }, ... etc } }Expression
String types[] = result .getAsStringArray("/results[0]/address_components[2]/types");
path
- Path expression to evaluateIllegalArgumentException
- on error traversing the document, ie.
traversing into an array without using subscripts.NumberFormatException
- if the value at path can not be converted
to an integer.public long[] getAsLongArray(String path) throws IllegalArgumentException
String types[] = result .getAsStringArray("/results[0]/address_components[2]/types");
path
- Path expression to evaluateIllegalArgumentException
- on error traversing the document, ie.
traversing into an array without using subscripts.NumberFormatException
- if the value at path can not be converted
to a long.public double[] getAsDoubleArray(String path) throws IllegalArgumentException
String types[] = result .getAsStringArray("/results[0]/address_components[2]/types");
path
- Path expression to evaluateIllegalArgumentException
- on error traversing the document, ie.
traversing into an array without using subscripts.NumberFormatException
- if the value at path can not be converted
to a double.public boolean[] getAsBooleanArray(String path) throws IllegalArgumentException
String types[] = result .getAsStringArray("/results[0]/address_components[2]/types");
path
- Path expression to evaluateIllegalArgumentException
- on error traversing the document, ie.
traversing into an array without using subscripts.public List getAsArray(String path) throws IllegalArgumentException
{ "results" : [ { "address_components" : [ { "long_name" : "921-989", "short_name" : "921-989", "types" : [ "street_number" ] }, { "long_name" : "Country Club Crescent", "short_name" : "Country Club Crescent", "types" : [ "route" ] }, ... etc } }Expression
List addressComponents = result.getAsList("/results[0]/address_components"); result = Result.fromContent(addressComponents); String longName = result.getAsString("[1]/long_name");
path
- Path expression to evaluateIllegalArgumentException
- on error traversing the document, ie.
traversing into an array without using subscripts.