0

I'm doing a get call and getting the response through InputStreamReader. So my string looks like this.

"["a", "b", "c"]"

I need to iterate through that string. I've tried making into arraylist using List, JSONArray, but cannot seem to loop through.

I rarely work with Java so I'm lost. Please help.

2 Answers 2

1

Below logic will help to loop the string array,

String[] input = {"a", "b", "c"};  

          List<String> stringList = Arrays.asList(input);  

          for (String str : stringList) {  
             System.out.println(str);  
             // your logic on looped string
          }  
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I replaced [] with "" and split it with , and then created list like your example.
1

Is that the whole response? Typically, you get a JSON or XML object or something of that sort in the response, and you can use some existing libraries to parse fields from it for you. See if that is the case.

If not, then it is simply a raw String. In that case, you need to use String operations to parse it. Use String.split(", ") which should give you a String array with 3 strings: ["a", "b", "c"]. you can use Arrays.asList() on this to get an ArrayList from this.

1 Comment

Thank you, I did use your answer and one below to solve it.

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.