How to create a JSONArray with GSON

前端 未结 3 607
暖寄归人
暖寄归人 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:34

    the class:

    public class JsonStructure{
        public String[] message;
        public String[] asec;
    }
    

    using it:

    JsonStructure json = new JsonStructure();
    json.message = new String[]{"msg 1", "msg 2", "msg 3"};
    json.asec = new String[]{"asec 1", "asec 2", "asec 3"};
    Gson gson = new Gson();
    String output = gson.toJson(json);
    //convert from string
    JsonStructure fromString = gson.fromJson(output, JsonStructure.class);
    

    For more details on Google's gson please take a look at https://sites.google.com/site/gson/gson-user-guide

提交回复
热议问题