将字符串转换为ArrayList <String>

Leo*_*eon 2 string android arraylist


我知道这是一个奇怪的问题,但我需要一件事:我收到一个字符串作为http操作的结果.实际上,这个字符串包含一个像这样的字符串数组:

["Leon","Prova 2","TestData"]
Run Code Online (Sandbox Code Playgroud)


基本上我想将此字符串转换为ArrayList,因为我需要它用于适配器.这是java代码:

public class Archivio{

    static ArrayList<String> titoli = new ArrayList<String>();
    static ArrayList<String> art = new ArrayList<String>();
    static String response, response2 = null;
    HttpClient httpclient;
     HttpPost httppost,httppost2;
     List<NameValuePair> nameValuePairs,namePairs;
    @SuppressLint({ "NewApi", "NewApi", "NewApi" })
    Archivio() {

        try {

            StrictMode.ThreadPolicy policy = new
                    StrictMode.ThreadPolicy.Builder()
                    .permitAll().build();
                    StrictMode.setThreadPolicy(policy);

                    httpclient=new DefaultHttpClient();
                     httppost= new HttpPost("http://tripleleon.altervista.org/artiview2.php"); 
                     nameValuePairs = new ArrayList<NameValuePair>();
                      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                     //Esecuzione richiesta HTTP Post e ricezione risultato operazione
                     //response=httpclient.execute(httppost);
                     ResponseHandler<String> responseHandler = new BasicResponseHandler();
                     response = httpclient.execute(httppost, responseHandler);
                     System.out.println("Response : " + response); 
                     httppost2= new HttpPost("http://tripleleon.altervista.org/artiview3.php"); 
                     namePairs = new ArrayList<NameValuePair>();
                      httppost2.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                     ResponseHandler<String> responseHandler2 = new BasicResponseHandler();
                     response2 = httpclient.execute(httppost2, responseHandler2);
                     System.out.println("Response : " + response2); 

        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    ArrayList<String> retTitle() {
        return response;
    }


    ArrayList<String> retArt() {
        return response2;   
    }
}
Run Code Online (Sandbox Code Playgroud)

nKn*_*nKn 5

尝试用下面的代码(即stringArrayString包含您的"阵"):

ArrayList<String> myList = new ArrayList<String>(Arrays.asList(stringArray.substring(1, stringArray.length() - 1).replaceAll("\"", "").split(",")));
Run Code Online (Sandbox Code Playgroud)

这将删除第一个和最后一个字符([]分别)并按,字符分割.然后,将其转换为ArrayList使用该Arrays.asList方法.