Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

希望支持链式嵌套json数组解析为List<T> #64

Open
dododcdc opened this issue May 19, 2022 · 4 comments
Open

希望支持链式嵌套json数组解析为List<T> #64

dododcdc opened this issue May 19, 2022 · 4 comments
Labels
bug Something isn't working enhancement New feature or request

Comments

@dododcdc
Copy link

比如我的数据类型是这样的:

{"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);

目前无法接收,会报如下错误:
image

@troyzhxu
Copy link
Owner

troyzhxu commented May 19, 2022

  • 对象 ArraytoList() 方法当前只支持简单的对象,还不支持对象中内嵌 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();

@troyzhxu troyzhxu added bug Something isn't working enhancement New feature or request labels May 19, 2022
@dododcdc
Copy link
Author

你好,我自己尝试研究了一下,还是没接收到
image
image
image

@dododcdc dododcdc reopened this May 19, 2022
@troyzhxu
Copy link
Owner

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

@dododcdc
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants