1

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.

3

2 Answers 2

1

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]
Sign up to request clarification or add additional context in comments.

1 Comment

That does look similar to what I was looking for, thanks. Jackson does have a lot of boilerplate, but I guess that's OK.
0

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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.