java反序列化多个数组

问题描述 投票:0回答:1

我有以下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
    }   ] }

如何将其反序列化为单个对象或数组,其中包含 Java 中的用户数据数组和 pokemon 数据数组?最好在 gson 中,但我不反对更改库。

java json gson
1个回答
0
投票

添加 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);

© www.soinside.com 2019 - 2024. All rights reserved.