Orh*_*nC1 2 android gson retrofit
当我尝试使用Retrofit解析以下JSON时,我最终得到了null成员对象.
解析:
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(CallerInfo.API_URL)
.setLogLevel(RestAdapter.LogLevel.FULL)
.build();
InGameInfo igi = restAdapter.create(InGameInfo.class);
Game game = igi.fetchInGameInfo("EUW", "sasquatching");
Log.d("Cancantest", "Game " + game); //Not null
Log.d("Cancantest", "Team one " + game.getTeamOne()); //Null
Run Code Online (Sandbox Code Playgroud)
游戏类:
@SerializedName("teamTwo")
@Expose private Team teamTwo;
@SerializedName("teamOne")
@Expose private Team teamOne;
public void setTeamOne(Team teamOne) {
this.teamOne = teamOne;
}
public void setTeamTwo(Team teamTwo) {
this.teamTwo = teamTwo;
}
public Team getTeamOne() {
return teamOne;
}
public Team getTeamTwo() {
return teamTwo;
}
Run Code Online (Sandbox Code Playgroud)
团队课程:
@SerializedName("array")
@Expose private TeamMember[] teamMembers;
public void setTeamMembers(TeamMember[] teamMembers) {
this.teamMembers = teamMembers;
}
public TeamMember[] getTeamMembers() {
return teamMembers;
}
Run Code Online (Sandbox Code Playgroud)
示例JSON:
{
"game":{
"teamTwo":{
"array":[]
},
"teamOne":{
"array":[]
}
}
}
Run Code Online (Sandbox Code Playgroud)
JSON包含顶级"游戏"条目,因此您无法直接反序列化游戏实例.您需要另一种类型,其具有Game表示响应的类型字段.
public class Response {
public final Game game;
public Response(Game game) {
this.game = game;
}
}
Run Code Online (Sandbox Code Playgroud)
您可以将JSON放在一个字符串中,并直接使用Gson来测试如何反序列化响应.这种行为几乎与Retrofit无关,而与Gson的行为有关.
String data = "...";
Game game = gson.fromJson(data, Game.class);
Response response = gson.fromJson(data, Response.class);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4174 次 |
| 最近记录: |