游戏类:
public class Game {
private String name;
private int steam_appid;
private boolean isInstalled;
}
Json示例:https://store.steampowered.com/api/appdetails/?appids=435150&filters=basic
我的尝试
public static Game readJson(String gameID) throws IOException {
String targetURL = String.format(STEAM_API, gameID);
URL url = new URL(targetURL);
InputStreamReader reader = new InputStreamReader(url.openStream());
Gson gson = new Gson();
Game json = gson.fromJson(reader, Game.class);
return json;
}
Name:null steam_appid:0 isInstalled:false
success
,如果值为true
则将其解析为Game
。否则,什么都不做。Game
?答案 0 :(得分:0)
如果字段标记为 transient (默认),则该字段将被忽略,并且不包含在JSON序列化或反序列化中。
https://sites.google.com/site/gson/gson-user-guide#TOC-Finer-Points-with-Objects
答案 1 :(得分:0)
如果不想在json中包含名称,请使用以下代码:
public class Game
{
private transient String name;
private int steam_appid;
private boolean isInstalled;
}
答案 2 :(得分:0)
选项1: 忽略字段级别的字段
public class Game {
private String name;
@JsonIgnore
private int steam_appid;
private boolean isInstalled;
}
我们也可以直接通过@JsonIgnore批注直接忽略该字段:
选项2:
另外,您可以使用 @JsonIgnoreProperties 注释跳过pojo属性。这是代码段:可以使用任何一种方法。
@JsonIgnoreProperties({ "name", "steam_appid" })
public class Game {
private String name;
private int steam_appid;
private boolean isInstalled;
}
答案 3 :(得分:0)
根据响应示例,要使用的Gson
类应如下所示:
请注意,这些类需要添加getter和setter。
class SteamResponseWrapper{
@SerializedName("434150")
private SteamResponse steamResponse;
}
class SteamResponse{
boolean success;
GameData data;
}
class GameData {
String name;
int steam_appid;
// I couldn't find the isInstalled property if you are adding it, make it transient
}
使用此结构,您将调用:
SteamResponseWrapper wrapper = new Gson().fromJson(reader, SteamResponseWrapper.class)
然后
wrapper.getSteamResponse().isSuccess()
检查成功字段。
还有
wrapper.getSteamResponse().getData()
来访问游戏对象。
但是我看到的问题是,顶级对象名称实际上是数据,并且对于每个游戏都会改变。因此,我认为Gson无法处理此问题,因为对于每个请求,带注释的序列化名称都应该不同。
我认为您最好使用Jackson或JsonObject来处理它。
编辑我
此示例使用以下Maven依赖项:
<dependency>
<groupId>javax.json</groupId>
<artifactId>javax.json-api</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.0.4</version>
</dependency>
示例:
public class SteamApiReader {
private static final String STEAM_API = "http:// ......";
public static void main(String args[]) {
try {
SteamApiReader.readJson("435150");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Game readJson(String gameID) throws IOException {
Game rtn = null;
String targetURL = String.format(STEAM_API, gameID);;
URL url = new URL(targetURL);
InputStreamReader reader = new InputStreamReader(url.openStream());
JsonReader jsonReader = Json.createReader(reader);
JsonStructure jsonStructure = jsonReader.read();
JsonObject jsonObject = (JsonObject) jsonStructure;
JsonObject wrapper = jsonObject.getJsonObject(gameID);
boolean success = wrapper.getBoolean("success");
if(success) {
JsonObject data = wrapper.getJsonObject("data");
String name = data.getString("name");
int steamAppId = data.getInt("steam_appid");
rtn = new Game(name, steamAppId);
}
return rtn;
}
public static class Game{
public Game(String name, int steamAppId) {
this.name = name;
this.steamAppId = steamAppId;
}
String name;
int steamAppId;
}
}