3

I'm looking for a java class that is able to represent a dynamic javascript object. This is so that I can move from and to Json while providing easy access to the data. I would like this class to have access methods similar to what you would expect in a javascript object.

Preferences p = new Preferences(...);

p.getString("a");
p.getInteger("a");
p.getObject("a");

All of the above would cast to the appropriate type or return null. Now the key part should be able to be something like this:

p.getString("a.b.c");
p.getString("a[0].b.d[1]");

And so on. Is there something already done that handles like this?

2 Answers 2

3

If you're trying to translate between JSON and Java, Gson is a good bet.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 for Gson, it's a very good lib. To represent an arbitrary JS/JSON object with it, try JsonObject
1

You can use HashMaps.. JavaScript object are just a little more than that..

If you need the extra methods for casting etc, you can write your own class JsObject or similarly named, which wraps a HashMap and provides those methods.

If all you want is to serialize deserialize between JSON and Java object try Jackson.

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.