gpt4 book ai didi

java - 解析从 API 获取的对象列表

转载 作者:行者123 更新时间:2023-12-01 17:10:23 25 4
gpt4 key购买 nike

您好,我正在使用 Retrofit 通过 Spring Boot 调用我的 API。

API响应

    [
{
"name": "whishky",
"price": 1000
},
{
"name": "vodka",
"price": 200
}
]

我的 pojo 类看起来像

public class MyResponse {
List<MyObject> resp;
}

MyObject 类看起来像

public class MyObject implements Serializable {

@JsonProperty("name")
private String name;

@JsonProperty("price")
private Double price;
}

API调用

Call<MyResponse> saveRequestCall = MyApi.fetchData(request);
Response<MyResponse> execute = saveRequestCall.execute();

现在的问题是当我调用 API 时出现异常

2020-04-25 18:08:18,895 ERROR c.s.e.b.XYZServiceImpl Error in fetching datawith exception com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `zzz.MyResponse` out of START_ARRAY token
at [Source: (InputStreamReader); line: 1, column: 1]

任何有关我如何解析此内容的帮助将不胜感激。

最佳答案

问题似乎映射到 MyResponse 因为它会出现类似的情况

{
"resp": [
{
"name": "whishky",
"price": 1000
},
...
]
}

如果你使用它应该被修复

Call<List<MyObject>> saveRequestCall = MyApi.fetchData(request);
Response<List<MyObject>> execute = saveRequestCall.execute();

关于java - 解析从 API 获取的对象列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61432928/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com