3

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;
}
Prabu
  • 3,550
  • 9
  • 44
  • 85
  • Why dont you try https://code.google.com/p/google-gson/ – sanket Dec 27 '13 at 13:06
  • possible duplicate of [conversion of array list to json object string](http://stackoverflow.com/questions/17359830/conversion-of-array-list-to-json-object-string) – BJ Myers Jun 07 '15 at 04:44

3 Answers3

9

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

Tareq Salah
  • 3,720
  • 4
  • 34
  • 48
2

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

JsonArray myCustomArray = gson.toJsonTree(list).getAsJsonArray();
Sujith PS
  • 4,776
  • 3
  • 34
  • 61
Saurabh
  • 7,525
  • 4
  • 45
  • 46
0

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();
Sujith PS
  • 4,776
  • 3
  • 34
  • 61