Java 创建 JSONObject

Java Creating JSONObject

我有一个像这样的 Class,它将包含数据:

public class PlayerData
{
    public ItemID id;
    public ClientHome home;
    public ClientAvatar avatar;
    public Instant creationDate;
    public int sessionCount;
    public int playTimeSeconds;
    public int daysSinceStart;
    public int startupCooldown;
}

客户端头像:

public class ClientAvatar extends Avatar
{
    protected ItemID m_id;
    protected ItemID m_baseId;
    protected ItemID m_clanId;
//  protected byte m_clanName[];
//  protected int m_clanBadge;
//  protected int m_clanRole;
    protected ItemID m_leagueId;
    protected ItemID m_lastLeagueId;
    protected int m_league;
    protected byte m_playerName[];
    protected int m_level;
    protected int m_xp;
    protected int m_gemCount;
    protected int m_freeGemCount;
    protected int m_trophyCount;
    protected int m_attacksWon;
    protected int m_attacksLost;
    protected int m_defencesWon;
    protected int m_defencesLost;
    protected ItemID m_unknown3;
    protected boolean m_nameSetByUser;
    protected int m_gemsBought;
    protected int m_nameChangesRemaining;
    protected byte m_playerFacebookId[];
    protected byte m_playerGameCenterId[];
    protected byte m_playerGoogleServiceId[];

客户主页:

public class ClientHome extends Base
{
    private ItemID m_id;
//  private BaseJson m_json;
    private String m_json;
    private int m_shieldSeconds;
    private Level m_level;

我尝试以 JSON 格式显示这些值,如下所示:

PlayerData playerData = DataManager.getPlayerData(1);
JSONObject player = new JSONObject(playerData);         
response = player.toString(4);

实际输出: {}

为什么会这样,我该如何解决?

谢谢。

您需要为所有属性添加 getter 和 setter。所以将 PlayerData class 更改为:

public class PlayerData
{
    private ItemID id;
    private ClientHome home;
    private ClientAvatar avatar;
    private Instant creationDate;
    private int sessionCount;
    private int playTimeSeconds;
    private int daysSinceStart;
    private int startupCooldown;

    public ItemID getId()
    {
        return id;
    }
    public void setId(ItemID id)
    {
        this.id = id;
    }
    public ClientHome getHome()
    {
        return home;
    }
    public void setHome(ClientHome home)
    {
        this.home = home;
    }
    public ClientAvatar getAvatar()
    {
        return avatar;
    }
    public void setAvatar(ClientAvatar avatar)
    {
        this.avatar = avatar;
    }
    public Instant getCreationDate()
    {
        return creationDate;
    }
    public void setCreationDate(Instant creationDate)
    {
        this.creationDate = creationDate;
    }
    public int getSessionCount()
    {
        return sessionCount;
    }
    public void setSessionCount(int sessionCount)
    {
        this.sessionCount = sessionCount;
    }
    public int getPlayTimeSeconds()
    {
        return playTimeSeconds;
    }
    public void setPlayTimeSeconds(int playTimeSeconds)
    {
        this.playTimeSeconds = playTimeSeconds;
    }
    public int getDaysSinceStart()
    {
        return daysSinceStart;
    }
    public void setDaysSinceStart(int daysSinceStart)
    {
        this.daysSinceStart = daysSinceStart;
    }
    public int getStartupCooldown()
    {
        return startupCooldown;
    }
    public void setStartupCooldown(int startupCooldown)
    {
        this.startupCooldown = startupCooldown;
    }
}

对所有要转换为 JSON 的 class 执行此操作,它应该可以工作。您需要为 ItemID、ClientHome 等执行此操作