Pawanpreet Singh last month

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 last month

Can be done using separator, where s is String:

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


SANJAY GUPTA last month

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 last month

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 last month

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 last month
Viewed 1,113 times
Voted 14
Answered 4 times

Search




Leave an answer