Java JSONObjects在JSONArray中循环

时间:2015-10-09 23:18:58

标签: java json

我需要一个像这样的输出JSON:

{
    "id": 171799578198,
    "name": "Forum Striking",
    "players": [
        {
          "avatar": {
            "userId": 21393,
            "currentHomeId": 21393,
            "clanId": 171799578198
          }
        },
        {
          "avatar": {
            "userId": 64425223942,
            "currentHomeId": 64425223942,
            "clanId": 171799578198
          }
        },
        {
          "avatar": {
            "userId": 17180463537,
            "currentHomeId": 17180463537,
            "clanId": 171799578198
          }
        }
    ]
}

每个"avatar":部分中的值将通过循环获得。我将如何形成这种JSON?

我有第一位:

{
  "id": 171799578198,
  "name": "Forum Striking"
}

但我不确定我需要做什么来获得球员名单。

我正在使用Java-JSON库。

1 个答案:

答案 0 :(得分:1)

你应该创建一个游戏,一个玩家和一个头像对象。游戏必须有玩家列表,玩家必须拥有头像对象。

填写您的游戏对象并通过new Gson().toJson(game)将其转换为Json,如下所示。

Gson是一个Json库(Google的项目),它使开发人员在使用Json时更轻松;)

public static void main(String[] args) throws IOException {

    Game game = new Game();
    game.setId(171799578198L);
    game.setName("Forum Striking");

    Avatar avatar = new Avatar();
    avatar.setUserId(21393L);
    avatar.setClanId(171799578198L);
    avatar.setCurrentHomeId(21393L);

    Player player = new Player();
    player.setAvatar(avatar);
    game.addPlayers(player);

    avatar = new Avatar();
    avatar.setUserId(64425223942L);
    avatar.setClanId(171799578198L);
    avatar.setCurrentHomeId(64425223942L);

    player = new Player();
    player.setAvatar(avatar);
    game.addPlayers(player);

    System.out.println(new Gson().toJson(game));
}

打印:

{
  "id": 171799578198,
  "name": "Forum Striking",
  "players": [
    {
      "avatar": {
        "userId": 21393,
        "currentHomeId": 21393,
        "clanId": 171799578198
      }
    },
    {
      "avatar": {
        "userId": 64425223942,
        "currentHomeId": 64425223942,
        "clanId": 171799578198
      }
    }
  ]
}

游戏对象,

public class Game {

    private Long id;
    private String name;
    private List<Player> players;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Player> getPlayers() {
        return players;
    }

    public List<Player> addPlayers(Player player) {
        if (getPlayers() == null)
            setPlayers(new ArrayList<Player>());
        players.add(player);
        return players;
    }

    public void setPlayers(List<Player> players) {
        this.players = players;
    }
}

玩家对象,

public class Player {
    private Avatar avatar;

    public Avatar getAvatar() {
        return avatar;
    }

    public void setAvatar(Avatar avatar) {
        this.avatar = avatar;
    }
}

头像对象

public class Avatar {
    private Long userId;
    private Long currentHomeId;
    private Long clanId;

    public Long getUserId() {
        return userId;
    }
    public void setUserId(Long userId) {
        this.userId = userId;
    }
    public Long getCurrentHomeId() {
        return currentHomeId;
    }
    public void setCurrentHomeId(Long currentHomeId) {
        this.currentHomeId = currentHomeId;
    }
    public Long getClanId() {
        return clanId;
    }
    public void setClanId(Long clanId) {
        this.clanId = clanId;
    }
}