This is a fork of the original JSON Java implementation made by Douglas Crockford.
With this fork it is possible to find an object or type using a path to the type instead of just a key. This gives means to find a single string from a complex JSON structure in just one line. The nodes are separated with a (dot) and array index surrounded by square braces. The array selection can even be an expression giving the possibility to find a particular object and a type within.
For all examples below this is the JSON we are using and it is parsed into a JSONObject called jsonObject:
{
"array": [
{
"id": 12,
"address": {
"zip": 12345
},
"name": "value1"
},
{
"id": 2,
"name": "value2"
},
{
"id": 1,
"address": {
"zip": 45678
},
"name": "value3"
}
]
}
String name = jsonObject.getString("array[0].name");
Name contains "value1" after the statement has been run.
int id = jsonObject.getInt("array[2].id");
The variable id contains 1 after this statement.
String name = jsonObject.getString("array[address.zip>20000].name");
Name contains "value3" after the statement has been run.
String name = jsonObject.getString("array[address.zip!=12345].name");
Name contains "value3" after the statement has been run.
String name = jsonObject.getString("array[id==2].name");
In case of arrays this syntax can be used:
jsonObject.getString("first[element**operand**value].second[element.element**operand**value].string");
These test operands can be used:
== : Equality test
!= : Not equal test
< : Less than test
> : Greater than test
<= : Less than or equal test
>= : Greater than or equal test
*= : Contains text test
!* : Not contains text test
^= : Starts with text test
!^ : Not starts with text test
$= : Ends with text test
!$ : Not ends with text test
The value used after the operand can have the pipe (|) character to separate several alternatives. Maybe this doesn't make sense with the comparators <, >, <=, >=, ^= and $= but for the others it's a quite neat feature.
int id = jsonObject.getInt("array[name!=value1|value3].id");
The variable id contains 2 after this statement.
String name = jsonObject.getString("array[id!=1|2].name");
The variable name contains value1 after this statement.
Some times you could come across JSON data which has keys that contains a dot or brackets. You can solve that by using the new setter for the path search specific characters:
jsonObject.setSearchByPathChars('/', '{', '}');
String name = jsonObject.getString("array{id!=1|2}/name");
The characters can be changed to any valid chars
The only important thing to know is that the characters you choose does not exist in any key and that they has to be different from each other.
jsonObject.setSearchByPathChars('§', '`', '\'');
String value = jsonObject.getString("array.json`0'§crazy{}/key");
JSON in Java [package org.json]
Douglas Crockford
[email protected]
2011-02-02
JSON is a light-weight, language independent, data interchange format.
See http://www.JSON.org/
The files in this package implement JSON encoders/decoders in Java.
It also includes the capability to convert between JSON and XML, HTTP
headers, Cookies, and CDL.
This is a reference implementation. There is a large number of JSON packages
in Java. Perhaps someday the Java community will standardize on one. Until
then, choose carefully.
The license includes this restriction: "The software shall be used for good,
not evil." If your conscience cannot live with that, then choose a different
package.
The package compiles on Java 1.2 thru Java 1.4.
JSONObject.java: The JSONObject can parse text from a String or a JSONTokener
to produce a map-like object. The object provides methods for manipulating its
contents, and for producing a JSON compliant object serialization.
JSONArray.java: The JSONObject can parse text from a String or a JSONTokener
to produce a vector-like object. The object provides methods for manipulating
its contents, and for producing a JSON compliant array serialization.
JSONTokener.java: The JSONTokener breaks a text into a sequence of individual
tokens. It can be constructed from a String, Reader, or InputStream.
JSONException.java: The JSONException is the standard exception type thrown
by this package.
JSONString.java: The JSONString interface requires a toJSONString method,
allowing an object to provide its own serialization.
JSONStringer.java: The JSONStringer provides a convenient facility for
building JSON strings.
JSONWriter.java: The JSONWriter provides a convenient facility for building
JSON text through a writer.
CDL.java: CDL provides support for converting between JSON and comma
delimited lists.
Cookie.java: Cookie provides support for converting between JSON and cookies.
CookieList.java: CookieList provides support for converting between JSON and
cookie lists.
HTTP.java: HTTP provides support for converting between JSON and HTTP headers.
HTTPTokener.java: HTTPTokener extends JSONTokener for parsing HTTP headers.
XML.java: XML provides support for converting between JSON and XML.
JSONML.java: JSONML provides support for converting between JSONML and XML.
XMLTokener.java: XMLTokener extends JSONTokener for parsing XML text.