gpt4 book ai didi

android - 如何使用动态 key 在改造中解析 json

转载 作者:太空宇宙 更新时间:2023-11-03 11:24:45 25 4
gpt4 key购买 nike

我有这个响应,我想使用改造来制作它的模型类。我知道如何为每个特定对象创建模型类,但不知道如何为第一个父数组创建模型类。

[3]
0: {
user: {
_id: "55e725b65656565d066037"
photo: "https://graph.facebook.com/9884898989882/picture?height=300&width=300"
provider: "facebook"
username: "xyz"
}
url:"abc.com"
}
1: {
user: {
_id: "55e725b65656565d066037"
photo: "https://graph.facebook.com/9884898989882/picture?height=300&width=300"
provider: "facebook"
username: "xyz"
}
url:"abc.com"
}
2: {
user: {
_id: "55e725b65656565d066037"
photo: "https://graph.facebook.com/9884898989882/picture?height=300&width=300"
provider: "facebook"
username: "xyz"
}
url:"abc.com"
}

最佳答案

如果你的 json 看起来像:

{
"0": {
"user": {
"_id": "55e725b65656565d066037",
"photo": "https://graph.facebook.com/9884898989882/picture?height=300&width=300",
"provider": "facebook",
"username": "xyz"
},
"url": "abc.com"
},
"1": {
"user": {
"_id": "55e725b65656565d066037",
"photo": "https://graph.facebook.com/9884898989882/picture?height=300&width=300",
"provider": "facebook",
"username": "xyz"
},
"url": "abc.com"
},
"2": {
"user": {
"_id": "55e725b65656565d066037",
"photo": "https://graph.facebook.com/9884898989882/picture?height=300&width=300",
"provider": "facebook",
"username": "xyz"
},
"url": "abc.com"
}
}

您可以为元素“0”、“1”等创建模型。

public class Element {

private User user;

private String url;

public User getUser() {
return user;
}

public String getUrl() {
return url;
}

public static class User {

@SerializedName("_id")
private String id;

private String photo;

private String provider;

private String username;

public String getId() {
return id;
}

public String getPhoto() {
return photo;
}

public String getProvider() {
return provider;
}

public String getUsername() {
return username;
}
}
}

最后你必须为顶级 json 级别创建 Map

Map<String, Element> elements;

您可以获得如下元素:

elements.get("0");

关于android - 如何使用动态 key 在改造中解析 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34103431/

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