Pawanpreet Singh January 2017

How to convert Stringified array to ArrayList in Android

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?

Answers


Gagan Sethi January 2017

Can be done using separator, where s is String:

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


SANJAY GUPTA January 2017

I think This is not right format of JSON. You can manually parse it.but being a good programmer this is not good idea.So please talk to your server side programmer to return right JSON format like below.

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


Scary Wombat January 2017

Modify your String using

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

so it is the same as

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

then use

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

    System.out.println(al);


Awadesh January 2017

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);

Post Status

Asked in January 2017
Viewed 1,113 times
Voted 14
Answered 4 times

Search




Leave an answer