简体   繁体   中英

Nested JSON object to java with GSON

Having issues deserializing a JSON response. The response received from the server is as follows:

{
"type": "success",
"message": {
"SERVER_INFO": {
  "map": "MP_Prison",
  "gameId": "360287970194292378",
  "gameExpansions": [
    0
  ],
  "mapMode": "64",
  "ip": "192.168.0.1",
  "matchId": "0",
  "protocolVersionString": "4202",
  "extendedInfo": {
    "message": "",
    "bannerUrl": "",
    "desc": ""
  },
  "game": 2048,
  "ranked": 1,
  "online": 1,
  "platform": 1,
  "updatedAt": 1402212153,
  "slots": {
    "8": {
      "current": 0,
      "max": 2
    },
    "1": {
      "current": 0,
      "max": 20
    },
    "2": {
      "current": 38,
      "max": 64
    }
  },
  "guid": "",
  "port": 25200,
  "punkbuster": 1,
  "playlist": 0,
  "gameExpansion": 0,
  "name": "",
  "settings": {
    "vgmc": 125,
    "vfrm": 0,
    "v3ca": 1,
    "vtbr": 100,
    "vaba": 1,
    "vmst": 2,
    "vmsp": 1,
    "v3sp": 1,
    "osls": 0,
    "vprt": 100,
    "vbdm": 100,
    "vvsa": 1,
    "vkca": 1,
    "vvsd": 100,
    "vffi": 0,
    "vtkk": 3,
    "vhit": 1,
    "vnit": 86400,
    "vrtl": 300,
    "vtkc": 5,
    "vhud": 1,
    "vmin": 1,
    "vrhe": 1,
    "vcmd": 0,
    "vshe": 100,
    "vnta": 1,
    "vrsp": 4,
    "vmpl": 0
  },
  "fairfight": 1,
  "region": 1,
  "mapVariant": 0,
  "ping": 999,
  "serverType": 2,
  "experience": "0",
  "hasPassword": 0,
  "maps": {
    "maps": [
      {
        "mapMode": "64",
        "map": "MP_Prison",
        "mapVariant": 0
      }
    ],
    "currentMapRound": 1,
    "nextMapIndex": 0,
    "currentMapIndex": 0,
    "numberMapRounds": 2
  },
  "secret": "",
  "preset": 1,
  "pingSite": "iad",
  "country": "us"
  }
 }
}

I have constructed the following class tree so to speak to deserialize to:

public class ServerInformation {
private String jsonStr;

public ServerInformation(String jsonStr) {
    this.jsonStr = jsonStr;
}

public Container deserialize(){
    Gson gson = new Gson();
    Container container = gson.fromJson(jsonStr, Container.class);
    return container;
}
class Container {
    private List<Message> message;
    private String type;
    private Data data;
}
class Message {
    @SerializedName("SERVER_INFO")
    private List<ServerInfo> serverInfo;
}
class ServerInfo {
    private String map;
    private int gameId;
    //private int[] gameExpansions;
    private int mapMode;
    private String ip;
    private int matchId;
    private int protocolVersionString;
    private ExtendedInfo extendedInfo;
    private int game;
    private int ranked;
    private int online;
    private int platform;
    private int updatedAt;
    private List<Slots> slots;
    private String guid;
    private int port;
    private int punkbuster;
    private int playlist;
    private int gameExpansion;
    private String name;
    private List<Settings> settings;
    private int fairfight;
    private int region;
    private int mapVariant;
    private int ping;
    private int serverType;
    private int experience;
    private int hasPassword;
    private List<Map> maps;
    private String secret;
    private int preset;
    private String pingSite;
    private String country;
}
class ExtendedInfo {
    private String message;
    private String bannerUrl;
    private String desc;
}
class Settings{
    private int vgmc;
    private int vfrm;
    private int v3ca;
    private int vtbr;
    private int vaba;
    private int vmst;
    private int vmsp;
    private int v3sp;
    private int osls;
    private int vprt;
    private int vbdm;
    private int vvsa;
    private int vkca;
    private int vvsd;
    private int vffi;
    private int vtkk;
    private int vhit;
    private int vnit;
    private int vrtl;
    private int vtkc;
    private int vhud;
    private int vmin;
    private int vrhe;
    private int vcmd;
    private int vshe;
    private int vnta;
    private int vrsp;
    private int vmpl;
}
class Slots{
    @SerializedName("1")
    private List<Integer> joining;
    @SerializedName("2")
    private List<Integer> numPlayers;
    @SerializedName("8")
    private List<Integer> numCommanders;
}
class Map{
    private List<Maps> maps;
    private int currentMapRound;
    private int nextMapIndex;
    private int currentMapIndex;
    private int numberMapRounds;
}
class Maps {
    private int mapMode;
    private String map;
    private int mapVariant;
}
class Data {
}
}

Please note that I have intentionally left out various amounts of information from the JSON response and getters to remove identifying information and keep the amount of space used to minimum.

My question is more along the lines of am I on the correct path with the use of the List type or am I headed in the wrong direction?

JSON to Java Object converion

JSON Type---------Java Type


Simple Type -------Primitive Variables
Arrays --------------- List
JSON Object------------Java Class (or Map is preferred if all elements of of JSON object are same type)

According to the above considerations

@SerializedName("SERVER_INFO")
private List<ServerInfo> serverInfo;

should not be List 

Design the classes accordingly

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM