skip to Main Content

I have the following json

{   "users": [
    {
      "id": 1,
      "name": "Ash",
      "email": "[email protected]"
    },
    {
      "id": 2,
      "name": "Gary",
      "email": "[email protected]"
    }],   
"pokemon": [
    {
      "userId": 1,
      "name": "Ember",
      "frontImg": "/images/pokemon/local-mon/ember/front.png",
      "backImg": "/images/pokemon/local-mon/ember/back.png",
      "type": "fire",
      "hp": 100,
      "id": 1
    },
    {
      "userId": 1,
      "name": "Draggy",
      "frontImg": "/images/pokemon/local-mon/dragon/front.png",
      "backImg": "/images/pokemon/local-mon/dragon/back.png",
      "type": "dragon",
      "hp": 100,
      "id": 2
    }   ] }

How could I deserialize this into a single object or array containing both the users array of data and the pokemon array of data in Java? Preferably in gson but I’m not opposed to changing libraries.

2

Answers


  1. Add the POJO classes:

    public class Example {
    
        @SerializedName("users")
        @Expose
        private List<User> users;
        @SerializedName("pokemon")
        @Expose
        private List<Pokemon> pokemon;
    
        public List<User> getUsers() {
            return users;
        }
    
        public void setUsers(List<User> users) {
            this.users = users;
        }
    
        public List<Pokemon> getPokemon() {
            return pokemon;
        }
    
        public void setPokemon(List<Pokemon> pokemon) {
            this.pokemon = pokemon;
        }
    }
    
    
    public class Pokemon {
    
        @SerializedName("userId")
        @Expose
        private Integer userId;
        @SerializedName("name")
        @Expose
        private String name;
        @SerializedName("frontImg")
        @Expose
        private String frontImg;
        @SerializedName("backImg")
        @Expose
        private String backImg;
        @SerializedName("type")
        @Expose
        private String type;
        @SerializedName("hp")
        @Expose
        private Integer hp;
        @SerializedName("id")
        @Expose
        private Integer id;
    
        public Integer getUserId() {
            return userId;
        }
    
        public void setUserId(Integer userId) {
            this.userId = userId;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getFrontImg() {
            return frontImg;
        }
    
        public void setFrontImg(String frontImg) {
            this.frontImg = frontImg;
        }
    
        public String getBackImg() {
            return backImg;
        }
    
        public void setBackImg(String backImg) {
            this.backImg = backImg;
        }
    
        public String getType() {
            return type;
        }
    
        public void setType(String type) {
            this.type = type;
        }
    
        public Integer getHp() {
            return hp;
        }
    
        public void setHp(Integer hp) {
            this.hp = hp;
        }
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
    }
    
    public class User {
    
        @SerializedName("id")
        @Expose
        private Integer id;
        @SerializedName("name")
        @Expose
        private String name;
        @SerializedName("email")
        @Expose
        private String email;
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getEmail() {
            return email;
        }
    
        public void setEmail(String email) {
            this.email = email;
        }
    
    }
    

    And then call Example e = new Gson().fromJson(json, Example.class);

    Login or Signup to reply.
  2. I suggest using Jackson to read JSON strings into Java Object without defining a suitable class.

    import com.fasterxml.jackson.databind.JsonNode;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.fasterxml.jackson.databind.SerializationFeature;
    import java.io.IOException;
    
    public class JavaApplication {
        public static void main(String[] args) throws IOException {
            ObjectMapper mapper = new ObjectMapper();
            mapper.enable(SerializationFeature.INDENT_OUTPUT);
    
            String json = "{"users":[{"id":1,"name":"Ash","email":"[email protected]"},{"id":2,"name":"Gary","email":"[email protected]"}],"pokemon":[{"userId":1,"name":"Ember","frontImg":"/images/pokemon/local-mon/ember/front.png","backImg":"/images/pokemon/local-mon/ember/back.png","type":"fire","hp":100,"id":1},{"userId":1,"name":"Draggy","frontImg":"/images/pokemon/local-mon/dragon/front.png","backImg":"/images/pokemon/local-mon/dragon/back.png","type":"dragon","hp":100,"id":2}]}";
            JsonNode data = mapper.readTree(json);
    
            System.out.println(mapper.writeValueAsString(data));
            System.out.println(data.get("users"));
            System.out.println(data.get("users").get(0));
            System.out.println(data.get("pokemon"));
            System.out.println(data.get("pokemon").get(0).get("name"));
        }
    }
    

    The output will be:

    {
      "users" : [ {
        "id" : 1,
        "name" : "Ash",
        "email" : "[email protected]"
      }, {
        "id" : 2,
        "name" : "Gary",
        "email" : "[email protected]"
      } ],
      "pokemon" : [ {
        "userId" : 1,
        "name" : "Ember",
        "frontImg" : "/images/pokemon/local-mon/ember/front.png",
        "backImg" : "/images/pokemon/local-mon/ember/back.png",
        "type" : "fire",
        "hp" : 100,
        "id" : 1
      }, {
        "userId" : 1,
        "name" : "Draggy",
        "frontImg" : "/images/pokemon/local-mon/dragon/front.png",
        "backImg" : "/images/pokemon/local-mon/dragon/back.png",
        "type" : "dragon",
        "hp" : 100,
        "id" : 2
      } ]
    }
    [{"id":1,"name":"Ash","email":"[email protected]"},{"id":2,"name":"Gary","email":"[email protected]"}]
    {"id":1,"name":"Ash","email":"[email protected]"}
    [{"userId":1,"name":"Ember","frontImg":"/images/pokemon/local-mon/ember/front.png","backImg":"/images/pokemon/local-mon/ember/back.png","type":"fire","hp":100,"id":1},{"userId":1,"name":"Draggy","frontImg":"/images/pokemon/local-mon/dragon/front.png","backImg":"/images/pokemon/local-mon/dragon/back.png","type":"dragon","hp":100,"id":2}]
    "Ember"
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search