0

I have a String like this:

["http://www.ebuy.al/Images/dsc/17470_500_400.jpg", "http://www.ebuy.al/Images/dsc/17471_500_400.jpg"]

How can I convert it into an ArrayList of Strings?

6
  • It looks suspicioulsy like JSON. Is it? Commented Sep 1, 2014 at 15:16
  • yes, it comes from a JSON service Commented Sep 1, 2014 at 15:18
  • What is the input of your data ? Commented Sep 1, 2014 at 15:18
  • 1
    Instead of rolling your own parser I'd suggest checking out a library like Jackson, GSON, or Retrofit. Commented Sep 1, 2014 at 15:19
  • Sorry, I didn't quite understand your question. Commented Sep 1, 2014 at 15:20

4 Answers 4

1

Use Arrays#asList

String[] stringArray = { "http://www.ebuy.al/Images/dsc/17470_500_400.jpg", "http://www.ebuy.al/Images/dsc/17471_500_400.jpg"}
List<String> stringList = Arrays.asList(stringArray);

In case your string contains braces [] and double quotes "", then you should parse the string manually first.

String yourString = "[\"http://www.ebuy.al/Images/dsc/17470_500_400.jpg\", \"http://www.ebuy.al/Images/dsc/17471_500_400.jpg\"]";
String[] stringArray = yourString
    .substring(1, yourString.length() - 2)
    .replace('"', '\0')
    .split(",\\s+");
List<String> stringList = Arrays.asList(stringArray);

Try the above if and only if you will always receive your String in this format. Otherwise, use a proper JSON parser library like Jackson.

Sign up to request clarification or add additional context in comments.

7 Comments

The String comes to me from the service as below ["http://www.ebuy.al/Images/dsc/17470_500_400.jpg", "http://www.ebuy.al/Images/dsc/17471_500_400.jpg"], now how should I convert into a string without quotations \" .
@Luigi from the service I get the exact string: ["http://www.ebuy.al/Images/dsc/17470_500_400.jpg", "http://www.ebuy.al/Images/dsc/17471_500_400.jpg"] Now I want the string to be converted into an arraylist of Strings , and each element to contains this kind of String : ebuy.al/Images/dsc/17470_500_400.jpg in order that I could use as an URL. None of your above methods doenst function
@Xhulio first of all, it's you who has to define what you exactly need. Second, we will provide help as far as what you have asked. If you don't ask for it, you won't get that help. I updated my answer to remove the quotes from the String. Note that it was a small modification to the current code that even you could have done if just put a little effort understanding the code rather than just copying/pasting it and hoping for a miracle to make it work for your specific unknown needs.
Sorry I didn't mean to be rude to u, I just got confused with the editing the comment. Apparently I gave the wrong impression :(
@Xhulio you don't look rude to me, but lazy, which I find even worse =\
|
0

This would be more appropriate

String jsonArr = "[\"http://www.ebuy.al/Images/dsc/17470_500_400.jpg\", 
                   \"http://www.ebuy.al/Images/dsc/17471_500_400.jpg\"]";
List<String> listFromJsonArray = new ArrayList<String>();
JSONArray jsonArray = new JSONArray(jsonArr);
for(int i =0 ;i<jsonArray.length();i++){
      listFromJsonArray.add(jsonArray.get(i).toString());
}

And don't forget to add json library

3 Comments

@LuiggiMendoza ya artifactId json and groupid org.json
With yours? I don't know. I haven't tested it.
That is what the OP need to do right as it appears like a json array
0

You can use simple CSV parser if you remove the first and last brackets ('[',']')

Something like this:

List<String> items = Arrays.asList(str.split("\\s*,\\s*"));

Comments

0

Method 1: Iterate through the array and put each element to arrayList on every iteration.

Method 2: Use asList() method

Example1: Using asList() method

String[] urStringArray = { "http://www.ebuy.al/Images/dsc/17470_500_400.jpg", "http://www.ebuy.al/Images/dsc/17471_500_400.jpg"}
List<String> newList = Arrays.asList(urStringArray);

Example2 Using simple iteration

List<String> newList = new ArrayList<String>();
for(String aString:urStringArray){
newList.add(aString);
}

Comments

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.