okhttps
okhttps copied to clipboard
希望支持链式嵌套json数组解析为List<T>
比如我的数据类型是这样的:
{"data":[
{
"created_at":"Sat May 14 22:05:52 +0800 2022",
"id": "4769148843065803",
"disable_reply": 0,
"comments":[
{
"created_at":"Sat May 14 22:09:52 +0800 2022",
"id": "4769148843065805",
"disable_reply": 1,
"comments":[]
},
{
"created_at":"Sat May 14 22:20:52 +0800 2022",
"id": "4769148843065807",
"disable_reply": 4,
"comments":[]
}
]
},
{
"省略":"省略"
}
]
}
我尝试建一个映射类去接收:
import java.util.List;
@Data
public class Comment {
//评论时间
private String created_at;
// 省略部分字段.......
// 该条评论下的评论
private List<Comment> comments;
}
我的接收处理:
List<Comment> cts= data.getArray("data").toList(Comment.class);
目前无法接收,会报如下错误:

-
对象
Array的toList()方法当前只支持简单的对象,还不支持对象中内嵌List的情况,这将在下个版本中增强它。 -
但是这个情况可以这么用,定义一个通用的结果类:
public class Result<T> {
private T data;
// 省略 Getter Setter
}
然后请求的时候:
// 使用复合泛型
Result<List<Comment>>> result = OkHttps.sync("/xxx").get().getBody()
.toBean(new TypeRef<Result<List<Comment>>>{});
// 得到 List<Comment>
List<Comment> comments = result.getData();
你好,我自己尝试研究了一下,还是没接收到

你多写了一步,是 getBody() 后就 toBean(), 不需要先 toMapper()
你多写了一步,是 getBody() 后就 toBean(), 不需要先 toMapper() 也报错了
