gpt4 book ai didi

java - 当Object-Key是随机的时,如何用Jackson2解析JSON?

转载 作者:行者123 更新时间:2023-11-30 06:14:45 26 4
gpt4 key购买 nike

我想用 Jackson2 解析 Java 中的 JSON,其结构如下:

{
"attachment": {
"_2K26Z-mLJmMSRnssLwD0zQ": {
"ext": "jpg",
"height": 3024,
"md5": "219c226e0070b7367f90e2f1bff1dfc2",
"name": "IMG_1871.jpg",
"ref": "MTUyMTAzNDY5MDUzNElNR18xODcxLmpwZw==",
"rotate": true,
"size": 1514957,
"thumb": "thumb_1024_219c226e0070b7367f90e2f1bff1dfc2",
"thumb_size": 73119,
"type": "image",
"width": 4032
},
"_Q7l14s87UquHcAYoolNCuw": {
"ext": "png",
"height": 186,
"md5": "75023fd60d59907376943bf109858336",
"name": "ns_attach_image_26071520280535225.png",
"ref": "MTUyMDI4MDUzNTIzMG5zX2F0dGFjaF9pbWFnZV8yNjA3MTUyMDI4MDUzNTIyNS5wbmc=",
"rotate": true,
"size": 15182,
"type": "image",
"width": 347
}
},
"title": "Test Title"
}

我的问题是我不知道如何处理附件的键,因为我无法定义具有未知名称字段的类。

有效的是

ObjectMapper mapper = new ObjectMapper();
Note note = mapper.readValue(fileio, Note.class);

类注释在哪里

public class Note {
private String title;
private Object attachment;

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public Object getAttachment() {
return attachment;
}

public void setAttachment(Object attachment) {
this.attachment = attachment;
}
}

但我想使用一个带有名称附件的类,该类包含 json 中键为“_2K26Z-mLJmMSRnssLwD0zQ”的所有字段。

最佳答案

您可以使用Map,这样附件的键就是映射中的键,映射中的值就是表示附件字段的对象。像这样:

public class Attachment {
private String ext;
private int height;
private String md5;
private String name;
private String ref;
private boolean rotate;
private int size;
private String thumb;
private int thumb_size;
private String type;
private int width;

}

public class Note {
private String title;

private Map<String, Attachment> attachment = new HashMap<>();
}

关于java - 当Object-Key是随机的时,如何用Jackson2解析JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49470304/

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