1

my response looks like following:

{"result":1,"redirect":"","user_login":"Woldo","user_id":"4358970","session_id":
"ab89JBFLrYJ,6-lXKnv-T9wC481"}

I only want the session_id part ab89JBFLrYJ,6-lXKnv-T9wC481 , I was using

ReadSession.substring(86, 113); 

but whenever someone with a longer username logs in I get in trouble because it is out of the 113 range.

thanks!

Mykola
  • 3,343
  • 6
  • 23
  • 39
Snake4life
  • 21
  • 2
  • 4
  • 1
    Use a JSON parser: [How to parse JSON in Java](http://stackoverflow.com/q/2591098) – Tom Jan 31 '16 at 14:40

3 Answers3

1

The data is in the JSON format. Use the JsonReader class to parse it, then extract the session_id value

PhilLab
  • 4,777
  • 1
  • 25
  • 77
1

Better to convert this input string into JsonObject and then extract session_id key from that object. If you are using org.json.JSONObject class then you can try this code

String input = //your input
  try {
      JSONObject jsonObject = new JSONObject(input);
      String sessionID = jsonObject.getString("session_id");
    }catch (JSONException e){

    }
thedarkpassenger
  • 7,158
  • 3
  • 37
  • 61
0

You could do something silly like this for general use

String string = "{\"result\":1,\"redirect\":\"\",\"user_login\":\"Woldo\",\"user_id\":\"4358970\",\"session_id\":\n" +
"\"ab89JBFLrYJ,6-lXKnv-T9wC481\"}";
       int index = string.indexOf("session_id");
       String id = string.substring(index+14, string.length()-2);
        System.out.println(id);
anaxin
  • 710
  • 2
  • 7
  • 16
  • thank you that was working for me very good, I might check out Json parsing later but for now its working with your solution thanks! – Snake4life Jan 31 '16 at 14:56