Access json nested data in one go?

I am trying to get a value from a json object. How do I get a third level json object:

The json format is as follows:

feedString = {"level1":[{"level2":{"level3":{"valueIWant":10}}}]} 

The code:

 JSONObject jsonFeed = new JSONObject(feedString); jsonFeed.get("level1.level2.level3.valueIWant"); 

Can I get nested levels at once? What should be my key?

+7
source share
3 answers

You can give JSONiJ (JSON in Java) ; it is a Java version of JSONPath and basically maps (a subset) of XPath syntax to JSON objects.

Also see this SO question for some other ideas; it looks like json-path has a java version and uses dot notation.

Another option is to create an EL bridge between JSONObjects and something like MVEL or OGNL, which will give you a more familiar point designation. (I thought there was a MVEL / JSON bridge, but it cannot find it now.)

+5
source

You must use JSONPath. Check out this Java implementation http://code.google.com/p/json-path/

+4
source

Some time has passed, but I have some good news. Just tried beanutils and it works like a charm! Assuming json is converted to a map: (any parser can do this)

 private Map<String, Object> json; 

All you need is:

 PropertyUtils.getProperty(json, "level1.level2.level3.valueIWant") 

Good luck :)

+2
source

All Articles