0

This is my Result where i got response from server that I want to get by Soap.I can parse this value by JSON but I'm having a problem doing so as I wish to get this value split.

Result=1~Saved successfully~{ "TABLE":[{ "ROW":[ { "COL":{ "UserID":"30068"}} ]}]}

I am using this code to get UserId values in tmpVal, however am not obtaining my desired results.

String tmpVal = returnValue.toString().split("~")[3];
Juxhin
  • 5,068
  • 8
  • 29
  • 55
  • 4
    Arrays in java are indexed from zero. You are trying to call the fourth of three elements after splitting. – azurefrog Aug 24 '14 at 11:45

2 Answers2

3

String tmpVal = returnValue.toString().split("~")[3];

This would give you the 4th String in the array produced by split, but since split only produced an array of 3 Strings, this code gives you an exception.

If you want to get the last part of the split response - { "TABLE":[{ "ROW":[ { "COL":{ "UserID":"30068"}} ]}]} - you need returnValue.toString().split("~")[2].

Of course, it would be safer to first test how many Strings were returned by split :

String[] splitResult = returnValue.toString().split("~");
if (splitResult.length > 2) {
    tempVal = splitResult[2];
}
Eran
  • 387,369
  • 54
  • 702
  • 768
  • 1
    then what will be the solution –  Aug 24 '14 at 11:44
  • 1
    Sorry This is not given me correct answer –  Aug 24 '14 at 11:53
  • 1
    This gives me { "TABLE":[{ "ROW":[ { "COL":{ "UserID":"30073"}} ]}]} –  Aug 24 '14 at 11:53
  • @amitsharma If you need the UserID, you have to continue spliting the result. – Eran Aug 24 '14 at 11:55
  • @amitsharma You can do, for example, `.split("\"UserID\":\"")[1]`, which will return `30068"}} ]}]}`. Then you can remove the remaining characters after the ID. – Eran Aug 24 '14 at 12:01
0

As stated above in the comment section, Arrays start with index 0, thus if you have an array with 3 elements, the index are 0..1..2 and not 1..2..3

All you have to do is change the String tmpVal = returnValue.toString().split("~")[3]; to:

String tmpVal = returnValue.toString().split("~")[2];

As that will obtain the 3rd element instead of the fourth element as you've been trying to do.

You may also check out this question

Community
  • 1
  • 1
Juxhin
  • 5,068
  • 8
  • 29
  • 55