使用GSON嵌套JSON对象到java

时间:2014-06-08 07:53:54

标签: java json gson

在反序列化JSON响应时出现问题。从服务器收到的响应如下:

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

我构建了以下类树,以便反序列化为:

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 {
}
}

请注意,我故意遗漏了JSON响应和getter中的各种信息,以删除识别信息并将使用的空间量保持在最低限度。

我的问题更多的是我在使用List类型的正确路径上,或者我朝错误的方向前进?

1 个答案:

答案 0 :(得分:0)

JSON to Java Object converion

JSON类型--------- Java类型


简单类型-------原始变量
数组---------------列表
JSON对象------------ Java类(如果JSON对象的所有元素都是相同类型,则首选Map)

根据以上考虑

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

should not be List 

相应地设计类