How to convert ArrayList to JSON object

前端 未结 5 771
遇见更好的自我
遇见更好的自我 2021-02-13 17:25
public ArrayList convertJsonToJsonObject(){

    ArrayList mylist = new ArrayList ();

    mylist.add(\"abc\");
    mylist.add(         


        
相关标签:
5条回答
  • 2021-02-13 17:51

    You can use the GSON library to accomplish this.

     ArrayList<String> mylist = new ArrayList<String> ();
     mylist.add("abc");
     mylist.add("cfd");
     mylist.add("ert");
     mylist.add("fg");
     mylist.add("ujk");
     String json = new Gson().toJson(mylist);
    

    You can refer to the GSON User Guide for more support.

    0 讨论(0)
  • 2021-02-13 17:53
    gson.toJson(object);
    

    Gson Google Group

    0 讨论(0)
  • 2021-02-13 17:59
    JSONArray jsArray = new JSONArray(mylist);
    

    Or

    Gson gson = new Gson();
    String jsonArray = gson.toJson(mylist);
    

    Get java-json.jar and Gson.jar here

    0 讨论(0)
  • 2021-02-13 18:00
    List<String> bibo = new ArrayList<String>();
    bibo.add("obj1");
    bibo.add("obj2");
    bibo.add("obj3");
    String json = new Gson().toJson(bibo);
    
    0 讨论(0)
  • 2021-02-13 18:02
    ArrayList<Domain> list = new ArrayList<Domain>();
    list.add(new Domain());
    list.add(new Domain());
    list.add(new Domain());
    String json = new Gson().toJson(list);
    
    0 讨论(0)
提交回复
热议问题