How to parse nested JSON using GSON

后端 未结 3 1419
自闭症患者
自闭症患者 2021-01-28 00:41

I am currently making a get request using volley and in onResponse i am trying to parse the jsonObject using gson to my model.

JSON Returned after the request is made:

相关标签:
3条回答
  • 2021-01-28 01:09

    i have a really easy approach for you:

    first of all create the main model: and the other one for "data"

    public class FeedBackModel {
        int success;
        String message;
        DataModel data;
    
    }
    
    public class DataModel {
    
        String company;
        String email;
        //etc ...
    }
    

    // create getter& setter for variables

    then when you got json, parse it like this:

                Gson gson = new Gson();
                Type CollectionType = new TypeToken<FeedBackModel>() {
                }.getType();
                FeedBackModel FeedBack = gson.fromJson(json, CollectionType);
    
    0 讨论(0)
  • 2021-01-28 01:22

    There is a little mistake in your code.So you should change ClientData clientData to ClientData data. Then you can get the data.Hope this will help you in parsing.

    0 讨论(0)
  • 2021-01-28 01:28

    As Mentioned by user1912026, change as below.

    private ClientData data;
    

    And change the setter and getter as below

    public ClientData getClientData() {
        return data;
    }
    
    public void setClientData(ClientData clientData) {
        this.data = clientData;
    }
    
    0 讨论(0)
提交回复
热议问题