Prabu
Prabu

Reputation: 3728

convert Arraylist into json

please help how could we convert the below Arraylist method into Json

public ArrayList<String> helloName(String patentno)  {

    ArrayList<String> bibo = new ArrayList<String>();

    for (int row = 2; row < rowsize; row++) {
        pno = driver.findElement(
                By.xpath("//*[@id='body']/table/tbody/tr[" + row + "]/td"))
                .getText();
        bibo.add(pno);
    }
    return bibo;
}

Upvotes: 3

Views: 28825

Answers (3)

Saurabh
Saurabh

Reputation: 7833

use
Gson gson = new GsonBuilder().create();

JsonArray myCustomArray = gson.toJsonTree(list).getAsJsonArray();

Upvotes: 2

Sujith PS
Sujith PS

Reputation: 4864

You can use org.codehaus.jettison.json.JSONArray for the same (Download jar).

Example Code :

   ArrayList bibo=new ArrayList();

   .....

  JSONArray ar=new JSONArray(bibo);
  String json= ar.toString();

Upvotes: 0

Tareq Salah
Tareq Salah

Reputation: 3718

Use GSON library for that. Here is the sample code

ArrayList<String> bibo = new ArrayList<String>();
bibo.add("obj1");
bibo.add("obj2");
bibo.add("obj3");
String json = new Gson().toJson(bibo);

And this example from Gson User Guide to use it on collection

Upvotes: 9

Related Questions