1

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!

AndroidDev
  • 20,466
  • 42
  • 148
  • 239
  • Because the json object was created in php and this has possible implications with the UTF-8 encoding. I edited the question to explain that. Thanks for pointing that out. Good call. – AndroidDev Aug 17 '13 at 05:07
  • 1
    @usr55410 its not valid JSON. – Yogesh Suthar Aug 17 '13 at 05:09
  • 2
    Your json is not valid. Use json validator : http://jsonlint.com/ – Hardik Joshi Aug 17 '13 at 05:11
  • it does not matter json string is created by which language JSON is Json –  Aug 17 '13 at 05:23
  • Not necessarily true: `http://stackoverflow.com/a/13370272/1024973` – AndroidDev Aug 17 '13 at 05:36
  • @usr55410 : FatalError is correct...JSON is JSON. Issues with string encoding is a separate thing. It doesn't matter whether it's JSON, XML or plain text strings you're working with - regardless of the source, if there's an encoding mismatch it'll screw things up. – Squonk Aug 17 '13 at 07:30

1 Answers1

0

you need to modify your JSONObject for parsing currently it is not in proper format.

{"result":"success",
"message":
{
"id":"1",
"first_name":"Tony",
"last_name":"Soprano",
"group":
{
    "group_id":"1",
    "group_name":"bada bing"
},
"email":"tony.soprano@gmail.com"
}
}

Use online tools like JSONPARSER

strike
  • 1,031
  • 8
  • 24
  • Your JSON is also invalid - remove the comma from after "bada bing" – Squonk Aug 17 '13 at 05:20
  • I'm so sorry. That comma is a typo. I was reducing the size of the actual array to keep the excess detail down. Thanks for the JSONPARSER suggestion. My string does evaluate to JSON. Still very confused. – AndroidDev Aug 17 '13 at 05:39
  • The problem was those quotes around the group object. I was encoding my inner json object inside of the php script. I needed to just leave it as an array, and only do the json encode once around the entire object. Thanks for your help. – AndroidDev Aug 17 '13 at 06:06