Java - Gson parsing nested within nested

后端 未结 1 383
遥遥无期
遥遥无期 2020-12-01 04:01

I have to interact with an API, and the response format (from what I\'ve read) seems to be poorly structured. I\'ve found a google groups reply to a somewhat similiar probl

相关标签:
1条回答
  • 2020-12-01 04:27

    The JSON objects {} can be represented by a Map<String, Object> or a Javabean class. Here's an example which uses a Javabean.

    public class ResponseData {
        private Response response;
        // +getter+setter
    
        public static class Response {
            private int reference;
            private Data data;
            // +getters+setters
        }
    
        public static class Data {
            private User user;
            // +getter+setter
        }
    
        public static class User {
            private String id;
            private String firstName; 
            private String lastName;
            private String email;
            private String phone;
            private Linkedid linkedid;
            // +getters+setters
        }
    
        public static class Linkedid {
            private String id;
            // +getter+setter
        }
    }
    

    Use it as follows:

    ResponseData responseData = new Gson().fromJson(json, ResponseData.class);
    
    0 讨论(0)
提交回复
热议问题