5

I am getting this from server

"[\"abc\",\"def\",\"ghi\",\"jkl\",\"mno\",\"pqr\",\"stu\",\"vwx\",\"yz\"]"

The above text is not an array, but a string returned from server.

I want to convert this in an ArrayList

Is there a way to convert it?

3
  • 3
    Why is it stringified? Just use JSON. And try using GSON to parse it to an array Commented Jan 12, 2017 at 7:49
  • which library u are using to parse JSON data, is it GSON ? Commented Jan 12, 2017 at 7:53
  • Actually I am getting this in my response from server { "error": false, "data": "[\"abc\",\"def\",\"ghi\",\"jkl\",\"mno\",\"pqr\",\"stu\",\"vwx\",\"yz\"]" "timeStamp": 1484203469568 } Commented Jan 12, 2017 at 9:32

7 Answers 7

3

There is no good idea to manually parse that string. You should use a library that parses JSON strings for you. Anyhow the given string is not a valid JSON string and like others have mentioned you should request JSON formatted data from the server.

If your server only returns like this and you need to manually parse then this would be a solution. Not a very good one, but it does the job.

public static void main(String[] args) {
    List<String> words = new ArrayList<>();
    String string = "[\"abc\",\"def\",\"ghi\",\"jkl\",\"mno\",\"pqr\",\"stu\",\"vwx\",\"yz\"]";

    String withoutBrackets = string.replaceAll("[\\[\\](){}]", ""); // Remove all the brackets
    for (String word : withoutBrackets.split(",")) {
        String singleWord = word.replaceAll("\"", "");
        words.add(singleWord);
    }

    System.out.println(words);
}
Sign up to request clarification or add additional context in comments.

1 Comment

This code would fail if one of the string contained a comma ","
3

Can be done using separator, where s is String:

List<String> myList = new ArrayList<String>(Arrays.asList(s.split(",")));

1 Comment

This code would also fail if one of the strings contained a comma ","
2

Try using Gson. Add this to your gradle

compile 'com.google.code.gson:gson:2.2.4'

Hope this helps -

String str = "[\"abc\",\"def\",\"ghi\",\"jkl\",\"mno\",\"pqr\",\"stu\",\"vwx\",\"yz\"]";
Gson gson=new Gson();
ArrayList<String> strings = gson.fromJson(str,new TypeToken<ArrayList<String>>(){}.getType());

Comments

1

This will work

String text = [\"abc\",\"def\",\"ghi\",\"jkl\",\"mno\",\"pqr\",\"stu\",\"vwx\",\"yz\"]";
text = text.replaceAll("[\\[\\](){}\"]", "");
List<String> list = Arrays.asList(text.split(","));

Comments

1

Modify your String using

     str = str.replace ("[", "").replace ("]", "");

so it is the same as

    String str = "\"abc\",\"def\",\"ghi\",\"jkl\",\"mno\",\"pqr\",\"stu\",\"vwx\",\"yz\"";

then use

    List<String> al = new ArrayList<String>(Arrays.asList(str.split(",")));

    System.out.println(al);

Comments

0

This is the correct way to parse JSON String to ArrayList :)

 ArrayList<String> list = new ArrayList<String>();
         String newArrayy ="[\"abc\",\"def\",\"ghi\",\"jkl\",\"mno\",\"pqr\",\"stu\",\"vwx\",\"yz\"]";
                  imagePath = newArrayy;
                            try {
                                JSONArray jsonArray = new JSONArray(imagePath);
                                if (null != jsonArray) {
                                    Logger.LogError("imagePathhh", jsonArray.toString() + "" + jsonArray.length());
 for (int i = 0; i < jsonArray.length(); i++) {           
    String value =(String) jsonArray.getString(i);
       list.add(value);                        dataBaseCurdOperation.insertPaymentPath(jsonArray.getString(i));
                                    }
                                } else {

                                }
                            } catch (Exception e) {

                            }
                        }

2 Comments

What these functions are doing in this hidingImage(imageCount); dataBaseCurdOperation.insertPaymentPath(jsonArray.getString(i));
these are my personal methods which i am using in my code :D
-1

Sanjay in his answer pointed it out correct , that it is not correct format.

Still if you are using Gson library to parse JSON data, then the following method take care of this format also. So you have no need to do anything :)

new Gson().fromJson(your_server_response, Model.class);

One more way to do this is using java's inbuilt method

public String replaceAll (String regularExpression, String replacement)

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.