public final class Result
- Object
- Result
An evaluator for a very small expression language to extract primitive types
from structured information. This implementation is layered over the
com.codename1.io.JSONParser and com.codename1.xml.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`
Fields
Methods
Inherited methods
Field details
JSON
public static final String JSON = "json"XML
public static final String XML = "xml"SEPARATOR
public static final char SEPARATOR = '/'ARRAY_START
public static final char ARRAY_START = '['ARRAY_END
public static final char ARRAY_END = ']'Method details
fromContent
public static Result fromContent(String content, String format)
throws IllegalArgumentExceptionParameters
contentString- structured content document as a string.
formatString- an identifier for the type of content passed (ie. xml, json, etc).
Returns
Throws
IllegalArgumentException- thrown if null content or format is passed.
fromContent
public static Result fromContent(InputStream content, String format)
throws IllegalArgumentException, IOExceptionCreate an evaluator object from a structured content document (XML, JSON, etc) input stream. Normally you would use this method within a content request implementation, for example:
ConnectionRequest request = new ConnectionRequest() {
protected void readResponse(InputStream input) throws IOException {
Result evaluator = Result.fromContent(input, Result.JSON);
// ... evaluate the result here
}
// ... etc
};
Parameters
contentInputStream- structured content document as a string.
formatString- an identifier for the type of content passed (ie. xml, json, etc).
Returns
Throws
IllegalArgumentException- thrown if null content or format is passed.
IOException
fromContent
public static Result fromContent(Reader content, String format)
throws IllegalArgumentException, IOExceptionCreate an evaluator object from a structured content document (XML, JSON, etc) input stream. Normally you would use this method within a content request implementation, for example:
ConnectionRequest request = new ConnectionRequest() {
protected void readResponse(InputStream input) throws IOException {
Result evaluator = Result.fromContent(input, Result.JSON);
// ... evaluate the result here
}
// ... etc
};
Parameters
contentReader- structured content document as a string.
formatString- an identifier for the type of content passed (ie. xml, json, etc).
Returns
Throws
IllegalArgumentException- thrown if null content or format is passed.
IOException
fromContent
public static Result fromContent(Element content)
throws IllegalArgumentExceptionParameters
contentElement- a parsed XML DOM.
Returns
Throws
IllegalArgumentException- thrown if null content is passed.
fromContent
public static Result fromContent(Map content)
throws IllegalArgumentExceptionParameters
contentMap- JSON content input stream
Returns
Throws
hashCode
public int hashCode()See also
equals
public boolean equals(Object other)See also
toString
public String toString()Returns
getAsBoolean
public boolean getAsBoolean(String path)
throws IllegalArgumentExceptionGet a boolean value from the requested path.
For example: JSON
{
"settings" : [
{
"toggle" : "true",
... etc
}
Expression
boolean value = result.getAsBoolean("/settings[0]/toggle");
Parameters
pathString- Path expression to evaluate
Returns
Throws
IllegalArgumentException- on error traversing the document, ie. traversing into an array without using subscripts.
getAsInteger
public int getAsInteger(String path)
throws IllegalArgumentExceptionGet an integer value from the requested path.
For example: JSON
{
"settings"
{
"connection"
{
"max_retries" : "20",
... etc
}
}
Expression
int value = result.getAsInteger("//connection/max_retries");
Parameters
pathString- Path expression to evaluate
Returns
Throws
IllegalException- on error traversing the document, ie. traversing into an array without using subscripts.
IllegalArgumentException
getAsLong
public long getAsLong(String path)
throws IllegalArgumentExceptionGet a long value from the requested path.
For example: JSON
{
"settings"
{
"connection"
{
"timeout_milliseconds" : "100000",
... etc
}
}
Expression
long value = result.getAsLong("/settings/connection/timeout_milliseconds");
Parameters
pathString- Path expression to evaluate
Returns
Throws
IllegalArgumentException- on error traversing the document, ie. traversing into an array without using subscripts.
getAsDouble
public double getAsDouble(String path)
throws IllegalArgumentExceptionGet a double value from the requested path.
For example: JSON
{
"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
}
}
// etc
Expression
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");
Parameters
pathString- Path expression to evaluate
Returns
Throws
IllegalArgumentException- on error traversing the document, ie. traversing into an array without using subscripts.
getAsString
public String getAsString(String path)
throws IllegalArgumentExceptionGet a string value from the requested path.
For example: JSON
{
"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");
Parameters
pathString- Path expression to evaluate
Returns
Throws
IllegalArgumentException- on error traversing the document, ie. traversing into an array without using subscripts.
get
public Object get(String path)
throws IllegalArgumentExceptionReturns
Throws
getSizeOfArray
public int getSizeOfArray(String path)
throws IllegalArgumentExceptionGet the size of an array at the requested path.
For example: JSON
{
"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");
Parameters
pathString- Path expression to evaluate
Returns
Throws
IllegalArgumentException- on error traversing the document, ie. traversing into an array without using subscripts.
getAsStringArray
public String[] getAsStringArray(String path)
throws IllegalArgumentExceptionGet an array of string values from the requested path.
For example: JSON
{
"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");
Parameters
pathString- Path expression to evaluate
Returns
Throws
IllegalArgumentException- on error traversing the document, ie. traversing into an array without using subscripts.
getAsIntegerArray
public int[] getAsIntegerArray(String path)
throws IllegalArgumentExceptionGet an array of values from the requested path.
For example: JSON
{
"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");
Parameters
pathString- Path expression to evaluate
Returns
Throws
IllegalArgumentException- 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.
getAsLongArray
public long[] getAsLongArray(String path)
throws IllegalArgumentExceptionGet an array of values from the requested path.
String types[] = result
.getAsStringArray("/results[0]/address_components[2]/types");
Parameters
pathString- Path expression to evaluate
Returns
Throws
IllegalArgumentException- 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.
getAsDoubleArray
public double[] getAsDoubleArray(String path)
throws IllegalArgumentExceptionGet an array of values from the requested path.
String types[] = result
.getAsStringArray("/results[0]/address_components[2]/types");
Parameters
pathString- Path expression to evaluate
Returns
Throws
IllegalArgumentException- 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.
getAsBooleanArray
public boolean[] getAsBooleanArray(String path)
throws IllegalArgumentExceptionGet an array of values from the requested path.
String types[] = result
.getAsStringArray("/results[0]/address_components[2]/types");
Parameters
pathString- Path expression to evaluate
Returns
Throws
IllegalArgumentException- on error traversing the document, ie. traversing into an array without using subscripts.
getAsShortArray
public short[] getAsShortArray(String path)
throws IllegalArgumentExceptionGet an array of short values from the requested path.
short values[] = result.getAsShortArray("//values");
Parameters
pathString- Path expression to evaluate
Returns
Throws
IllegalArgumentException- 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 short.
getAsFloatArray
public float[] getAsFloatArray(String path)
throws IllegalArgumentExceptionGet an array of float values from the requested path.
float values[] = result.getAsFloatArray("//values");
Parameters
pathString- Path expression to evaluate
Returns
Throws
IllegalArgumentException- 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 float.
getAsByteArray
public byte[] getAsByteArray(String path)
throws IllegalArgumentExceptionGet an array of byte values from the requested path.
byte values[] = result.getAsByteArray("//values");
Parameters
pathString- Path expression to evaluate
Returns
Throws
IllegalArgumentException- 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 byte.
getAsArray
public List getAsArray(String path)
throws IllegalArgumentExceptionGet a List of values from the requested path.
For example: JSON
{
"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");
Parameters
pathString- Path expression to evaluate
Returns
Throws
IllegalArgumentException- on error traversing the document, ie. traversing into an array without using subscripts.
mapNamespaceAlias
public void mapNamespaceAlias(String namespaceURI, String alias)