How to add a String Array in a JSON object?

后端 未结 5 716
孤城傲影
孤城傲影 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:28

    @bhavindesai's, answer is great answer. Here is another way to solve this issue. You can simply do it using Json Simple library. Here is the gradle

    compile 'com.googlecode.json-simple:json-simple:1.1'
    

    Here is sample code:

    org.json.simple.JSONObject jsonObject=new org.json.simple.JSONObject();
    jsonObject.put("Object","String Object");
    
    ArrayList list = new ArrayList();
                list.add("john");
                list.add("mat");
                list.add("jason");
                list.add("matthew");
    
                jsonObject.put("List",list);
    

    That's it. :)

提交回复
热议问题