应对json阵列中包装对象

发布于 2025-02-02 21:26:43 字数 2723 浏览 1 评论 0原文

我创建了一个抽象请求,以使其更容易发送请求。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文