应对json阵列中包装对象
我创建了一个抽象请求,以使其更容易发送请求。
abstract class MyRequest<T> extends Request<T> {
private final static String API_KEY = "";
private final static String API_STRING = "?api_key=";
private final Class<T> objectType;
private final Response.Listener<T> listener;
public MyRequest(String url, Class<T> objectType, Response.Listener<T> listener, @Nullable Response.ErrorListener errorListener) {
super(Method.GET, url + API_STRING + API_KEY, errorListener);
this.objectType = objectType;
this.listener = listener;
}
@Override
protected Response<T> parseNetworkResponse(NetworkResponse response) {
try {
return Response.success(Mapper.getMapper().readValue(new String(response.data, HttpHeaderParser.parseCharset(response.headers)), objectType),
HttpHeaderParser.parseCacheHeaders(response));
} catch (IOException e) {
return Response.error(new ParseError(e));
}
}
@Override
protected void deliverResponse(T response) {
listener.onResponse(response);
}
}
此代码用于制作这样的自定义请求:
public class MasteryRequest extends MyRequest<Summoner> {
private static final String URL = "https://euw1.api.riotgames.com/lol/champion-mastery/v4/champion-masteries/by-summoner/";
public MasteryRequest(String summonerId, Response.Listener<Summoner> listener, @Nullable Response.ErrorListener errorListener) {
super(URL + summonerId, Summoner.class, listener, errorListener);
}
}
现在我的问题是API返回一系列对象,而杰克逊无法将其转换为我的召唤仪对象。召唤仪对象看起来像这样:
public class Summoner {
private List<MasteryChampion> masteryChampions;
public List<MasteryChampion> getMasteryChampions() {
return masteryChampions;
}
}
杰克逊似乎不知道如何将返回值放入召唤师的数组中。我是否缺少任何注释,所以杰克逊知道如何将阵列放入召唤师?
响应主体示例:
[
{
"championId": 92,
"championLevel": 7,
"championPoints": 1029885,
"lastPlayTime": 1653428885000,
"championPointsSinceLastLevel": 1008285,
"championPointsUntilNextLevel": 0,
"chestGranted": true,
"tokensEarned": 0,
"summonerId": "03tFEbQfU5-pL1y8xR0XBckv6bJx1M6OtfVe8WKQtc-_AFc"
},
{
"championId": 114,
"championLevel": 7,
"championPoints": 274271,
"lastPlayTime": 1653418873000,
"championPointsSinceLastLevel": 252671,
"championPointsUntilNextLevel": 0,
"chestGranted": true,
"tokensEarned": 0,
"summonerId": "03tFEbQfU5-pL1y8xR0XBckv6bJx1M6OtfVe8WKQtc-_AFc"
}
]
I have created an abstract request to make it easier to send requests.
abstract class MyRequest<T> extends Request<T> {
private final static String API_KEY = "";
private final static String API_STRING = "?api_key=";
private final Class<T> objectType;
private final Response.Listener<T> listener;
public MyRequest(String url, Class<T> objectType, Response.Listener<T> listener, @Nullable Response.ErrorListener errorListener) {
super(Method.GET, url + API_STRING + API_KEY, errorListener);
this.objectType = objectType;
this.listener = listener;
}
@Override
protected Response<T> parseNetworkResponse(NetworkResponse response) {
try {
return Response.success(Mapper.getMapper().readValue(new String(response.data, HttpHeaderParser.parseCharset(response.headers)), objectType),
HttpHeaderParser.parseCacheHeaders(response));
} catch (IOException e) {
return Response.error(new ParseError(e));
}
}
@Override
protected void deliverResponse(T response) {
listener.onResponse(response);
}
}
This code is used to make a custom request like this:
public class MasteryRequest extends MyRequest<Summoner> {
private static final String URL = "https://euw1.api.riotgames.com/lol/champion-mastery/v4/champion-masteries/by-summoner/";
public MasteryRequest(String summonerId, Response.Listener<Summoner> listener, @Nullable Response.ErrorListener errorListener) {
super(URL + summonerId, Summoner.class, listener, errorListener);
}
}
Now my problem is the API returns an array of objects and jackson is unable to convert that into my Summoner object. The summoner object looks like this:
public class Summoner {
private List<MasteryChampion> masteryChampions;
public List<MasteryChampion> getMasteryChampions() {
return masteryChampions;
}
}
It seems like jackson doesn't know how to put the return value into the array of summoner. Is there any annotation that I am missing so jackson knows how to put the array into summoner?
Response body example:
[
{
"championId": 92,
"championLevel": 7,
"championPoints": 1029885,
"lastPlayTime": 1653428885000,
"championPointsSinceLastLevel": 1008285,
"championPointsUntilNextLevel": 0,
"chestGranted": true,
"tokensEarned": 0,
"summonerId": "03tFEbQfU5-pL1y8xR0XBckv6bJx1M6OtfVe8WKQtc-_AFc"
},
{
"championId": 114,
"championLevel": 7,
"championPoints": 274271,
"lastPlayTime": 1653418873000,
"championPointsSinceLastLevel": 252671,
"championPointsUntilNextLevel": 0,
"chestGranted": true,
"tokensEarned": 0,
"summonerId": "03tFEbQfU5-pL1y8xR0XBckv6bJx1M6OtfVe8WKQtc-_AFc"
}
]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论