Reputation: 6120
I'm wondering if anyone knows of any popular Java libraries for creating and parsing JSON strings the way it's done in Javascript. I'm not looking for a mapping library or anything of that sort. I'm starting to think that working with JSON the way Javascript works might be a better alternative.
I'm looking for something that has a builder pattern for creating JSON (since we don't have multiline strings), and a way to extract elements using simple strings for the keys and also supports easily deep "linking" into the JSON string.
Thanks in advance.
Upvotes: 1
Views: 182
Reputation: 1909
Check out JSONPath. It makes it simple to query JSON structures
List<String> authors = JsonPath.read(json, "$.store.book[*].author");
There is a Java implementation available here http://code.google.com/p/json-path/
Upvotes: 0
Reputation: 7834
Jackson's tree model may be similar to what you are asking for. Tatu has plans for adding JSONPath in the future as well. I use Jackson for several projects and can vouch for its speed and stability.
Of course, any JSON parser that creates simple hashmaps and lists can be accessed quite easily:
{"topkey":{"leafkey":["a", "b", "c"],"leafkey2":"blancmange"},"topkey2":42}
Java:
data.get("topkey").get("leafkey").get(1);
vs. JavaScript:
data["topkey"]["leafkey"][1]
Upvotes: 1