我有一个像这样的类,它将包含数据:
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;
}
ClientAvatar:
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[];
ClientHome:
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);
实际输出:
{}
为什么会这样,我该如何解决?
感谢。
答案 0 :(得分:0)
您需要为所有属性添加getter和setter。所以将PlayerData类更改为:
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的所有类执行此操作,它应该可以正常工作。您需要为ItemID,ClientHome等执行此操作。