0

I get a response but it's different for every user. I want to extract the UserID & Name. How can I do this?

The response String:

{
    "sessionkey":"f4b54dedlfkjhgdlfkjghdfslkjgh1a255242bf71597f4b35ac882f0702",
    "user" : {
        "userID" : "1",
        "name":"Test"
    }
}

I want to save 1 = (String userid) and save Test = (String name)

Thanks in advance

Matt Clark
  • 27,671
  • 19
  • 68
  • 123

1 Answers1

0

This is just a simple JSON blob, so use a JSON Parser to extract the data.

My lib of choice is org.json.json.

String blob = "{ origin string }";

// create a new json object from the main blob
JSONObject root = new JSONObject(blob);

// extract the session key
String sessionKey = root.getString("sessionkey");

// extract the user object
JSONObject user = root.getJSONObject("user");

// extract the user id
String userId = user.getString("userID");
Matt Clark
  • 27,671
  • 19
  • 68
  • 123
  • Hey Matt, thanks for the quick reply! How do I add this lib? I see what you did with the code, it's just not accepting it. – Bobby de Jong Feb 23 '17 at 20:48
  • If you are using maven, add the dependency blob. If you are not using maven, download the jar and add it to your classpath. I've linked to the artifact at mvncentral. – Matt Clark Feb 23 '17 at 20:49