java - Gson.toJson() misordering items -


i have output 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????" } 

but problem players array comes , part of initial details left out.

this code:

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 = is.readboolean();          if(a){             is.readint();             is.readint();         }          int memcount = is.readint();         /// members!!          int = 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;     } 

you can find details game, playeravatar , gameplayer class in post: https://stackoverflow.com/a/33048622

does have idea on how can ordered properly?

gson create json string according object's fields declaration order (even if not guaranteed because "reflection ordering may change in jdk 9" in future:), works.)

be sure jdk min 6. gson min 2.2.4 version.

private long id; private string name; private list<player> players; 

prints json string: first id, name players.

{   "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; 

prints json string: id, players , name @ end.

{   "id": 171799578198,   "players": [     {       "avatar": {         "userid": 21393,         "currenthomeid": 21393,         "clanid": 171799578198       }     },     {       "avatar": {         "userid": 64425223942,         "currenthomeid": 64425223942,         "clanid": 171799578198       }     }   ],   "name": "forum striking" } 

so should declare player list @ end of game object.


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -