[英]How to get and ignore specific field in JSON
Game class: 游戏类别:
public class Game {
private String name;
private int steam_appid;
private boolean isInstalled;
}
Json example: https://store.steampowered.com/api/appdetails/?appids=435150&filters=basic Json示例: https ://store.steampowered.com/api/appdetails/ ? appids = 435150 & filters = basic
My attempt: 我的尝试:
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
Name:null steam_appid:0 isInstalled:false
success
first, if value is true
parse it to Game
. success
,如果value为true
,则将其解析为Game
。 Otherwise, do nothing. Game
? Game
? If a field is marked transient , (by default) it is ignored and not included in the JSON serialization or deserialization. 如果一个字段被标记为transient ,(默认情况下),它将被忽略并且不包含在JSON序列化或反序列化中。
https://sites.google.com/site/gson/gson-user-guide#TOC-Finer-Points-with-Objects https://sites.google.com/site/gson/gson-user-guide#TOC-Finer-Points-with-Objects
use below code if you don't want to include name in json: 如果您不想在json中包含名称,请使用以下代码:
public class Game
{
private transient String name;
private int steam_appid;
private boolean isInstalled;
}
Option 1: Ignore Field at the Field Level 选项1: 在字段级别忽略字段
public class Game {
private String name;
@JsonIgnore
private int steam_appid;
private boolean isInstalled;
}
We can also ignore a field directly via the @JsonIgnore annotation directly on the field: 我们也可以直接通过@JsonIgnore批注直接忽略该字段:
Option 2: In another way you can use @JsonIgnoreProperties annotation to skip pojo properties. 选项2:您可以使用@JsonIgnoreProperties注释以另一种方式跳过pojo属性。 Here is the code snippet: You can use either way.
这是代码段:可以使用任何一种方法。
@JsonIgnoreProperties({ "name", "steam_appid" })
public class Game {
private String name;
private int steam_appid;
private boolean isInstalled;
}
Based on the response example the class to use Gson
should look like this: 根据响应示例,使用
Gson
的类应如下所示:
Note the classes need the getters and setters to be added. 请注意,这些类需要添加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
}
With this structure you would call: 通过这种结构,您将调用:
SteamResponseWrapper wrapper = new Gson().fromJson(reader, SteamResponseWrapper.class)
and then 接着
wrapper.getSteamResponse().isSuccess()
to check the success field. 检查成功字段。
And 和
wrapper.getSteamResponse().getData()
to access the game object. 访问游戏对象。
But the problem I see is that the top object name is in fact data and will change for every game. 但是我看到的问题是,顶级对象名称实际上是数据,并且对于每个游戏都会改变。 So I don't think Gson will be able to handle this because for every request the annotated serialized name should be different.
因此,我认为Gson无法处理此问题,因为对于每个请求,带注释的序列化名称都应该不同。
I think you will be better of handling it with Jackson or as a JsonObject. 我认为您最好使用Jackson或JsonObject来处理它。
EDIT I 编辑我
This example uses these maven dependencies: 此示例使用以下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>
The example: 这个例子:
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.