I feel dumb but I\'ve been looking around for this for a while. I\'m working with the google geocoder API and I need a bit of help with the json responses. Here is a JSONObje
you can extend the JSONObject class and override the public Object get(String key) throws JSONException
with the following:
public Object get(String key) throws JSONException {
if (key == null) {
throw new JSONException("Null key.");
}
Object object = this.opt(key);
if (object == null) {
if(key.contains(".")){
object = this.getWithDotNotation(key);
}
else
throw new JSONException("JSONObject[" + quote(key) + "] not found.");
}
return object;
}
private Object getWithDotNotation(String key) throws JSONException {
if(key.contains(".")){
int indexOfDot = key.indexOf(".");
String subKey = key.substring(0, indexOfDot);
JSONObject jsonObject = (JSONObject)this.get(subKey);
if(jsonObject == null){
throw new JSONException(subKey + " is null");
}
try{
return jsonObject.getWithDotNotation(key.substring(indexOfDot + 1));
}catch(JSONException e){
throw new JSONException(subKey + "." + e.getMessage());
}
}
else
return this.get(key);
}
Please feel free to better handle the exceptions.. im sure it's not handled correctly. Thanks