使用Gson将Json转换为Java对象
我目前正在使用Steam Web API的项目。 API以JSON格式返回数据,我想创建一个使用此数据的Java程序。以下是API中的以下JSON格式:
I am currently working on a project where I am using the Steam Web API. The API returns data in JSON format and I wanted to make a Java program that uses this data. Here is the following JSON format from the API:
{
"response": {
"players": [
{
"steamid": "---",
"communityvisibilitystate": 1,
"profilestate": 1,
"personaname": "---",
"lastlogoff": 1429915502,
"profileurl": "---",
"avatar": "---",
"avatarmedium": "---",
"avatarfull": "---",
"personastate": 0
}
]
}
}
我使用的Google JSON API称为Gson, Java类,以便我可以使用 fromJson()
方法。
I am using Google's JSON API called Gson and I am having trouble setting up the Java classes so that I can use the fromJson()
method.
是包含所有数据的玩家
对象的数组。让我困惑的一个事情是外部标签 response
。我知道我必须构造一个代表玩家
对象的类,但是我还必须创建一个代表响应
因为它包含玩家
?
From the JSON data, I know that there is an array of players
objects that contain all the data. The one thing that is confusing me is the outer tag called response
. I know that I have to construct a class representing the players
objects but do I also have to create a class that represents response
since it encloses players
?
到目前为止,我有一个名为 Response.java
的文件,其中包含以下内容:
As of right now, I have a file named Response.java
that contains the following:
public class Response {
private ArrayList<Player> playerSummaries = new ArrayList<Player>();
public String toString() {
return playerSummaries.get(0).toString();
}
}
class Player {
private String steamid;
private String personaname;
public String getSteamID() {
return steamid;
}
public void setSteamID(String newSteamID) {
steamid = newSteamID;
}
public String getPersonaName() {
return personaname;
}
public void setPersonaName(String name) {
personaname = name;
}
//Rest of getters and setters omitted.
@Override
public String toString() {
return "<<" + "steamid=" + steamid + "\n" + "name=" + personaname + "\n" + ">>";
}
}
我只包括我打算使用的变量。上面的JSON数据包含在一个名为 jsonData
的字符串中,这是我在我的主要方法中测试:
I only included the variables that I plan to use. The JSON data above is contained in a String called jsonData
and this is what I am testing in my main method:
Response response = gson.fromJson(jsonData, Response.class);
System.out.println(response.getPlayerAt(0));
但是,运行这给我一个IndexOutOfBoundsException。似乎Gson无法获取信息并将其存储到对象中?谁能告诉我一些光我的这个问题?
However, running this gives me an IndexOutOfBoundsException. It seems as if Gson was unable to get the information and store it into an object? Could anyone shed some light on this problem I am having? I am mostly curious to know if I have set up my classes correctly.
替换
private ArrayList<Player> playerSummaries = new ArrayList<Player>();
private ArrayList<Player> players = new ArrayList<Player>();
GSON使用反射查找应填充的字段。在你的情况下,它是查找你是否有一个名为玩家
的字段,而不是。
GSON uses reflection to look up which field it should populate. In your case it is looking up whether you have a field named players
which you do not.
您也不需要实例化字段,GSON将为您执行此操作。
You also do not need to instantiate the field, GSON will do that for you.
EDIT:
您还需要一个环绕顶级对象的包装类。所以
You also need a wrapper class around your top-level object. So
class MyObject {
public Response response;
}
MyObject myObject = gson.fromJson(jsonData, MyObject.class);