How to add a String Array in a JSON object?

后端 未结 5 738
孤城傲影
孤城傲影 2021-01-03 19:49

I am trying to create this JSON object on android. I am stuck on how to add a string array in the object.

A = {
    \"class\" : \"4\" ,
    \"name\" : [\"joh         


        
5条回答
  •  耶瑟儿~
    2021-01-03 20:43

    Little improper approach suggested by Tom. Optimised code would be:

    ArrayList list = new ArrayList();
    list.add("john");
    list.add("mat");
    list.add("jason");
    list.add("matthew");
    
    JSONObject school = new JSONObject();
    
    school.put("class","4");
    school.put("name", new JSONArray(list));
    

提交回复
热议问题