【问题标题】:convert Arraylist into json将 Arraylist 转换为 json
【发布时间】:2014-01-15 01:52:53
【问题描述】:

请帮助我们如何将下面的 Arraylist 方法转换为 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;
}

【问题讨论】:

标签: java json arraylist


【解决方案1】:

使用
Gson gson = new GsonBuilder().create();

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

【讨论】:

    【解决方案2】:

    您可以使用 org.codehaus.jettison.json.JSONArray 来实现相同的效果 (Download jar)。

    示例代码:

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

    【讨论】:

      【解决方案3】:

      为此使用GSON 库。这是示例代码

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

      还有来自 Gson 用户指南的 this example 在收藏中使用它

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-28
        • 1970-01-01
        相关资源
        最近更新 更多