I'm trying to implement a function that given any JSONObject
and a path String
, would return the object's attribute corresponding to the path.
For example, given this json:
{
"name": "John",
"friends": [
{"name": "Paul",
"age":42},
{"name": "Peter",
"age":24}
],
"address": {"city": "London"}
}
getAttribute(jsonObject, "name")
should return"John"
getAttribute(jsonObject, "address.city")
should return"London"
getAttribute(jsonObject, "friends[0].name")
should return"Paul"
Note that this JSON is only an example, jsonObject
has no predefined structure and could represent any valid json.
I wrote a first version implementing the first two cases, but handling arrays and multi-level arrays "foo[0][0].bar"
brings a lot of complexity to this function.