英文:
Create Java objects more directly from JSON with JsonPath
问题
List<String> httpList = JsonPath.read(json, "$.*.http_url_to_repo");
List<String> idList = JsonPath.read(json, "$.*.id");
List<GitlabProject> back = IntStream.range(0, httpList.size())
.mapToObj(i -> new GitlabProject(httpList.get(i), idList.get(i)))
.collect(Collectors.toList());
英文:
I create Objects from JsonPath with
List<String> httpList = JsonPath.read(json, "$.*.http_url_to_repo");
List<String> idList = JsonPath.read(json, "$.*.id");
for (int i = 0; i < httpList.size(); i++)
{
back.add(new GitlabProject(httpList.get(i), idList.get(i)));
}
Is there a more direct way to create the GitlabProject
objects from the JSON?
The JSON looks like
[
{
"id": 63,
"description": null,
"name": "conti-maven-plugin-mm",
"name_with_namespace": "ik / spu / other / conti-maven-plugin-mm",
"path": "conti-maven-plugin-mm",
"path_with_namespace": "ik/spu/other/conti-maven-plugin-mm",
"created_at": "2023-01-30T12:33:59.218Z",
"default_branch": "main",
"tag_list": [],
"topics": [],
"ssh_url_to_repo": "git@gitlab-test.continentale.loc:ik/spu/other/conti-maven-plugin-mm.git",
"http_url_to_repo": "https://gitlab-test.continentale.loc/ik/spu/other/conti-maven-plugin-mm.git",
"web_url": "https://gitlab-test.continentale.loc/ik/spu/other/conti-maven-plugin-mm",
....
答案1
得分: 1
假设你的POJO具有不同的属性名称:
public class GitlabProject {
private int id;
@JsonProperty("http_url_to_repo")
private String httpUrlToRepo;
// 省略构造函数、getter和setter
}
使用Jackson库中的ObjectMapper将JSON数组反序列化为GitlabProject对象列表:
ObjectMapper objectMapper = new ObjectMapper();
List<GitlabProject> projects = objectMapper.readValue(json, new TypeReference<List<GitlabProject>>(){});
你也可以使用Google的GSON库来执行类似的操作:
List<GitlabProject> projects = new Gson().fromJson(json, new TypeToken<List<GitlabProject>>(){}.getType());
请注意,在这种情况下,如果属性名称不同,你需要使用@SerializedName
。
英文:
Assuming your POJO has different property names:
public class GitlabProject {
private int id;
@JsonProperty("http_url_to_repo")
private String httpUrlToRepo;
// Constructor, getters and setters omitted for brevity
}
Use ObjectMapper from the Jackson library to deserialize the JSON array into a list of GitlabProject objects:
ObjectMapper objectMapper = new ObjectMapper();
List<GitlabProject> projects = objectMapper.readValue(json, new TypeReference<List<GitlabProject>>(){});
You can do similar thing using Google's GSON library.
List<GitlabProject> projects = new Gson().fromJson(json, new TypeToken<List<GitlabProject>>(){}.getType());
Not that you'll need to use @SerializedName if property name is different in this case.
答案2
得分: 1
这可以直接使用 JsonPath 自身完成。只需定义一个自定义映射函数。
List<GitlabProject> projects = JsonPath.parse(json)
.read("$[*]", list -> list.stream()
.map(obj -> {
String httpUrl = JsonPath.read(obj, "$.http_url_to_repo");
int id = JsonPath.read(obj, "$.id");
return new GitlabProject(httpUrl, id);
})
.collect(Collectors.toList())
);
- "$[*]" - 选择JSON数组中的所有元素。
- "read" 方法内的 lambda 表达式从JSON中提取所有 "http_url_to_repo" 和 "id" 值,并创建相应的 "GitlabProject"。
- 最后,使用 "Collectors.toList()" 创建对象列表。
英文:
This can be done using JsonPath itself directly.
Just define a custom mapping function.
List<GitlabProject> projects = JsonPath.parse(json)
.read("$[*]", list -> list.stream()
.map(obj -> {
String httpUrl = JsonPath.read(obj, "$.http_url_to_repo");
int id = JsonPath.read(obj, "$.id");
return new GitlabProject(httpUrl, id);
})
.collect(Collectors.toList())
);
- "$[*]" - selects all elements in the JSON array
- The lamda expression inside "read" method extracts all
"http_url_to_repo" and "id" values from JSON and creates
corresponding "GitlabProject". - Finally the list of objects are created using
"Collectors.toList()"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论