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];
1
  • 4
    Arrays in java are indexed from zero. You are trying to call the fourth of three elements after splitting. Commented Aug 24, 2014 at 11:45

2 Answers 2

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];
}
Sign up to request clarification or add additional context in comments.

5 Comments

then what will be the solution
Sorry This is not given me correct answer
This gives me { "TABLE":[{ "ROW":[ { "COL":{ "UserID":"30073"}} ]}]}
@amitsharma If you need the UserID, you have to continue spliting the result.
@amitsharma You can do, for example, .split("\"UserID\":\"")[1], which will return 30068"}} ]}]}. Then you can remove the remaining characters after the ID.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.