gpt4 book ai didi

java - 如何使用响应格式的改造因参数而异?

转载 作者:搜寻专家 更新时间:2023-11-01 08:21:17 25 4
gpt4 key购买 nike

我有一个登录 api,其中响应格式根据登录用户的类型而有所不同。

“类型”= 4 的 Json 结构

{
"type": 4,
"user": {
"coord_id": 5,
"coord_login_id": 4,
"coord_name": "Randall Pfannerstill",
}
}

“类型”= 2 的 Json 格式

{
"type": 2,
"user": {
"stud_id": 1,
"stud_reference_id": "3",
}
}

我正在使用这个模型类来处理“type”2

public class LoginResponse {
@SerializedName("type")
public int type;
@SerializedName("user")
public User user;

public class User {
@SerializedName("stud_id")
public int stud_id;
@SerializedName("stud_reference_id")
public String stud_reference_id;

}

如何处理具有相同类的“type”= 4 的情况(或者我应该如何编写不同的模型类)

最佳答案

由于您的 JSON 有不同的键名,我建议您改用 Map:

public class YourPojo {
private int type;
private Map<String, String> user;
}

根据你的类型,你应该知道你从响应中得到了哪个User

关于java - 如何使用响应格式的改造因参数而异?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50541447/

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