How to create a JSONArray with GSON

前端 未结 3 701
暖寄归人
暖寄归人 2021-01-15 17:42

Hi in my project i need to create a JSONArray type with the GSON class

    {
  \"message\": [
    \"msg 1\",
    \"msg 2\",
    \"msg 3\"
  ],
  \"asec\": [         


        
3条回答
  •  我在风中等你
    2021-01-15 18:35

    public class ArrayBean {
        ArrayList message = new ArrayList();
        ArrayList asec = new ArrayList();
    
    }
    

    create bean class above arraybean, then u can add the values on your main class and print the output.

       Gson gson = new Gson();
       ArrayBean arrayBean = new ArrayBean();
    
            arrayBean.message.add("msg 1");
            arrayBean.message.add("msg 2");
            arrayBean.message.add("msg 3");
    
            arrayBean.asec.add("asec 1");
            arrayBean.asec.add("asec 2");
            arrayBean.asec.add("asec 3");
    
    
            Log.d("rvg",""+gson.toJson(arrayBean));
    

提交回复
热议问题