我有这个输出JSON:
{
"id": 42950262095,
"name": "lol",
"players": [
{
"avatar": {
"userId": 25771876384,
"userName": "yhht",
"role": "Leader",
"level": 40,
"league": 0,
"trophies": 1011,
"donatedTroops": 0,
"receivedTroops": 0
}
},
{
"avatar": {
"userId": 146035414262,
"userName": "ari",
"role": "New Member",
"level": 8,
"league": 0,
"trophies": 428,
"donatedTroops": 0,
"receivedTroops": 0
}
},
{
"avatar": {
"userId": 300659467521,
"userName": "cp 221",
"role": "New Member",
"level": 6,
"league": 0,
"trophies": 97,
"donatedTroops": 0,
"receivedTroops": 0
}
}
],
"badge": 13000049,
"status": "Anyone Can Join",
"playerCount": 3,
"score": 767,
"requiredTrophies": 0,
"warsWon": 0,
"warsLost": 0,
"warsTied": 0,
"warFrequency": 0,
"exp": 0,
"level": 1,
"description": "??lol????"
}
但问题是球员阵列来得太早,部分原始细节被遗漏了。
这是我的代码:
public void parseAvatar() throws IOException, JSONException{
Game game = new Game();
game.setId(is.readLong());
game.setName(is.readString());
game.setBadge(is.readInt());
game.setStatus(status(is.readInt()));
game.setPlayerCount(is.readInt());
game.setScore(is.readInt());
game.setRequiredTrophies(is.readInt());
game.setWarsWon(is.readInt());
game.setWarsLost(is.readInt());
game.setWarsTied(is.readInt());
is.readInt();
game.setWarFrequency(is.readInt());
is.readInt();
game.setExp(is.readInt());
game.setLevel(is.readInt());
game.setDescription(is.readString());
is.readInt();
boolean a = is.readBoolean();
if(a){
is.readInt();
is.readInt();
}
int memCount = is.readInt();
/// Members!!
int i = 0;
while(i < memCount){
PlayerAvatar avatar = new PlayerAvatar();
avatar.setUserId(is.readLong());
avatar.setUserName(is.readString());
avatar.setRole(role(is.readInt()));
avatar.setLevel(is.readInt());
avatar.setLeague(is.readInt());
avatar.setTrophies(is.readInt());
avatar.setDonatedTroops(is.readInt());
avatar.setReceivedTroops(is.readInt());
is.readInt();
is.readInt();
is.readLong();
is.readByte();
is.readByte();
is.readLong();
GamePlayer player = new GamePlayer();
player.setAvatar(avatar);
game.addPlayers(player);
i++;
}
json = new Gson().toJson(game);
System.out.println();
}
private String role(int role) {
String memberRole = "";
if(role == 1){
memberRole = "New Member";
}
if(role == 2){
memberRole = "Leader";
}
if(role == 3){
memberRole = "Elder";
}
if(role == 4){
memberRole = "Co Leader";
}
return memberRole;
}
private String status(int statusint) {
String type = null;
if(statusint == 1){
type = "Anyone Can Join";
}
if(statusint == 2){
type = "Invite Only";
}
if(statusint == 3){
type = "Closed";
}
return type;
}
您可以在这篇文章中找到Game,PlayerAvatar和GamePlayer课程的详细信息:https://stackoverflow.com/a/33048622
有没有人知道如何才能正确订购?
答案 0 :(得分:2)
根据定义,JSON对象是无序的 - 您不应该依赖属性的顺序。所以gson正在做的事情非常好。
如果由于某种原因你别无选择,只能依赖顺序,那么这个问题就有一些解决方案:How to keep fields sequence in Gson serialization(例如使用jackson代替gson并使用@JsonPropertyOrder
,或者实现自定义串行化器)。
使用当前 gson和JDK实现,重新排序类中的成员将按相应的顺序生成输出,但这是一个非常脆弱的解决方案。例如
返回数组中的元素未排序且不按任何特定顺序
HashMap
,订单会发生变化,您的代码也会中断。如果您只为自己的娱乐编写代码,或者只使用一次转换某些数据,那么这可能没什么大不了的,但如果您正在编写其他人的代码(甚至 Future You )需要维护,我强烈建议你编写不依赖于假设层的代码,和/或不断测试以确保多个未记录的行为没有改变。
现在花费几分钟时间整合代码以确保字段的排序可以节省数小时或数天的调试时间。这在现实生活中一直发生。
答案 1 :(得分:2)
Gson根据你的对象的字段声明顺序创建json字符串(即使不能保证,因为“将来可能会在JDK 9中改变反射排序:),它可以工作。”
确保你的JDK是min 6. Gson是min 2.2.4版本。
private Long id;
private String name;
private List<Player> players;
打印为json字符串:第一个id,name然后是player。
{
"id": 171799578198,
"name": "Forum Striking",
"players": [
{
"avatar": {
"userId": 21393,
"currentHomeId": 21393,
"clanId": 171799578198
}
},
{
"avatar": {
"userId": 64425223942,
"currentHomeId": 64425223942,
"clanId": 171799578198
}
}
]
}
private Long id;
private List<Player> players;
private String name;
打印为json string:id,玩家和名称。
{
"id": 171799578198,
"players": [
{
"avatar": {
"userId": 21393,
"currentHomeId": 21393,
"clanId": 171799578198
}
},
{
"avatar": {
"userId": 64425223942,
"currentHomeId": 64425223942,
"clanId": 171799578198
}
}
],
"name": "Forum Striking"
}
所以你应该在游戏对象的末尾声明玩家列表。