0

So I have a JSON object that looks like this:

{
    "accessToken" : "<dont need this>",
    "clientToken" : "<nor this>",
    "selectedProfile" : {
        "id" : "<nope>",
        "name" : "<I need this>",
        "legacy" : true
    },
    "availableProfiles" :
    [
        {
            "id" : "<not this>",
            "name" : "<not this>",
            "legacy" : true
        }
    ]
}

So what I need is selectedProfile > name. I am able to extract selected profiles, would I just repeat the process on that? What should I do to retrieve it?

Pshemo
  • 122,468
  • 25
  • 185
  • 269
Sodex234
  • 105
  • 1
  • 8
  • 2
    Possible duplicate of [How to parse JSON in Java](http://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) – Uma Kanth Dec 24 '15 at 14:07
  • Your question is very strange. You claim you are able to access key which doesn't exist in your JSON (unless by `selected profiles` you mean `availableProfiles`) but you can't access key which exist.. Can we see your code at least? Also what library are you using? – Pshemo Dec 24 '15 at 14:07
  • yes, show your existing code – Sabir Khan Dec 24 '15 at 14:08

2 Answers2

3

Using javax.json

 JsonReader reader = new JsonReader(yourString);
 JsonObject base = reader.readObject();
 JsonObject profile = base.getJsonObject("selectedProfile");
 String name = profile.getJsonString("name");

and then name should be the object you want.

William Rosenbloom
  • 2,506
  • 1
  • 14
  • 37
0

Try this:

String name = yourJSON.getJSONObject("selectedProfile").getString("name").toString();
Alvin
  • 894
  • 8
  • 16