0

Hi all I am getting the below response when execute a REST request.

["A103388","R101858","R5575"]  

but I need the values between the braces []. I tried using the split() and replace() but not able to achieve.

2 Answers 2

1

No Need to SPLIT . If you playing with JSONObject

JSONObject object = new JSONObject(response_message); //["A103388","R101858","R5575"]
for(int i = 0; i<object.length() ; i++)
{
    String getValue= object.get(i);
}

Another Way

 String getValue="your_respose";
 String [] getSplit=getValue.split(",");

        for(int i = 0; i < getSplit.length; i++)
        {

            System.out.println("Amiyo"+getSplit[i]);
        }
Sign up to request clarification or add additional context in comments.

1 Comment

wouldn't this be a JSONArray ?
0

In order to get the individual values, you can use the following code:

String input = "[\"A103388\",\"R101858\",\"R5575\"]"
String[] parsedInput = input.replaceAll("[\[\]]", "").split(",")

This code will replace all brackets with nothing, essentially removing it (input.replaceAll("[\[\]]", "")), after which it is split on the comma (.split(",")). This results in a String array, which has each of the requested elements as elements.

2 Comments

hi Max but my input does not contain the \ after the braces,
The backslashes are used to escape the brackets, which also have a meaning in Regular Expressions. With these backslashes, the brackets after them are interpreted literally.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.