I have a JSONObject that contains nested JSONObjects. I need to pull out the inner objects and get their values. My main object looks something like this:
{"result":"success",
"message":
{
"id":"1",
"first_name":"Tony",
"last_name":"Soprano",
"group":
"{
\"group_id\":\"1\",
\"group_name\":\"bada bing\"
}",
"email":"tony.soprano@gmail.com"
}
}
Trying to access the inner object "group" elements like this:
private void jsonToSharedPreferences(String jsonMessage) throws JSONException {
System.err.println(jsonMessage); //<--output looks perfect
JSONObject user = new JSONObject(jsonMessage);
String strGroup = user.getJSONObject("group").toString().substring(3);
JSONObject group = new JSONObject(strCoach.substring(strGroup.indexOf("{"), strGroup.lastIndexOf("}") + 1));
System.err.println(group.get("group_name").toString());
}
The reason for all of the substring stuff is that I have found posts on this issue that suggest that there is some UTF-8 encoding (coming from PHP where the object was created) that hides three characters at the beginning of the string. I've tried this code with with and without the substring. I get the same result.
Unfortunately, I get this error no matter what I do:
org.json.JSONException: Value {"group_id":"1","group_name":"bada bing"} at group of type java.lang.String cannot be converted to JSONObject
Does anyone know why my object can output perfectly as a string, but fail when I try to convert it to a JSONObject? I'm at a complete loss. Thanks!