gson java反序列化多个数组

hgtggwj0  于 2023-05-06  发布在  Java
关注(0)|答案(2)|浏览(159)

我有以下JSON

{   "users": [
    {
      "id": 1,
      "name": "Ash",
      "email": "ashketchum@kanto.com"
    },
    {
      "id": 2,
      "name": "Gary",
      "email": "garyoak@kanto.com"
    }],   
"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
    }   ] }

我如何在Java中将其反序列化为一个同时包含users数据数组和pokemon数据数组的对象或数组?最好是gson,但我不反对改变库。

5jdjgkvh

5jdjgkvh1#

添加POJO类:

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;
    }

}

然后调用Example e = new Gson().fromJson(json, Example.class);

bvpmtnay

bvpmtnay2#

我建议使用Jackson将JSON字符串读取到Java Object中,而无需定义合适的类。

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\":\"ashketchum@kanto.com\"},{\"id\":2,\"name\":\"Gary\",\"email\":\"garyoak@kanto.com\"}],\"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"));
    }
}

输出将是:

{
  "users" : [ {
    "id" : 1,
    "name" : "Ash",
    "email" : "ashketchum@kanto.com"
  }, {
    "id" : 2,
    "name" : "Gary",
    "email" : "garyoak@kanto.com"
  } ],
  "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":"ashketchum@kanto.com"},{"id":2,"name":"Gary","email":"garyoak@kanto.com"}]
{"id":1,"name":"Ash","email":"ashketchum@kanto.com"}
[{"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"

相关问题