gpt4 book ai didi

java - 将 JSON 对象映射到 Java 类

转载 作者:行者123 更新时间:2023-12-02 05:23:02 25 4
gpt4 key购买 nike

我有一个解析为 JSON 的文件系统结构:

{
"name": "rootf",
"type": "system",
"path": "Parsing/rootf",
"children": [{
"name": "f1",
"type": "folder",
"path": "Parsing/rootf/f1",
"children": [{
"name": "subf1",
"type": "folder",
"path": "Parsing/rootf/f1/subf1",
"children": [{
"name": "text1.txt",
"type": "file",
"path": "Parsing/rootf/folder1/subf1/text1.txt",
"children": ["a", "b", "c"]
}]
}, {
"name": "subf2",
"type": "folder",
"path": "Parsing/rootf/f1/subf2",
"children": []
}, {
"name": "text2.txt",
"type": "file",
"path": "TParsing/rootf/f1/text2.txt",
"children": ["d", "e", "f"]
}]
}, {
"name": "text1.txt",
"type": "file",
"path": "Parsing/rootd/text1.txt",
"children": ["aa", "bb"]
}],
"_id": "5ce47292d866fc2f40037a56"
}

可以看出children类型 system (表示根文件夹)且类型为 folder (表示根文件夹的子文件夹)可以包含其他文件夹和/或文件。 children类型 file包含文件的内容。

我稍后需要访问 folder 类型的单个对象并输入 file 。将其映射到 Java 对象的最有效方法是什么?我如何单独访问它们?

我最初尝试使用 GSON 将其映射到三个类,System.java , Folder.javaFile.java其中包含 private List<Folder> children , private List<File> childrenprivate List<String> children分别。仅当我有特定的 JSON 结构(RootFolder->Sub-Folder->File)时,这才有效。有什么方法可以使映射更加通用,以便它包含 System 的条件可以包含FolderFile类似地,a Folder可以包含FolderFile

最佳答案

JSON 中有一个问题,内容如果是文件或文件夹,则表示为 children,因此我尝试了一种替换 children JSON 的方法如果它具有使用正则表达式的字符串集合,其中没有任何 JSON 对象,则使用 file 键。完成后,我只需要一个类,即我的情况下的文件夹,将其解析为 JSON 到 java 类。
所以我的类(class)如下。

class Folder{  //Can be either system or folder
private String name;
private String type;
private String path;
private List<Folder> children; //To contain subFolder
private List<String> file; //To contain list of files
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public List<Folder> getChildren() {
return children;
}
public void setChildren(List<Folder> children) {
this.children = children;
}
public List<String> getFile() {
return file;
}
public void setFile(List<String> file) {
this.file = file;
}
@Override
public String toString() {
return "Folder [name=" + name + ", type=" + type + ", path=" + path + "\n, children=" + children + ", file="
+ file + "]";
}
}

转换 key 然后解析为此类的代码

public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
ObjectMapper objectMapper = new ObjectMapper();
String contents = new String(Files.readAllBytes(Paths.get("C:\\Users\\Sample.json"))); //Java 7 to read JSON into String from file

Pattern pattern = Pattern.compile("\"children\":\\s*\\[\\s*[\"a-zA-Z\",\\s*]*]",Pattern.MULTILINE); //Pattren to find list of file i.e. content
Matcher matcher = pattern.matcher(contents);
while(matcher.find()) {
String group = matcher.group();
String newGroup = matcher.group().replaceAll("\"children\"", "\"file\"");
contents = contents.replace(group, newGroup);
}
Folder folder = objectMapper.readValue(contents, Folder.class);
System.out.println(folder);

}

输出

Folder [name=rootf, type=system, path=Parsing/rootf
, children=[Folder [name=f1, type=folder, path=Parsing/rootf/f1
, children=[Folder [name=subf1, type=folder, path=Parsing/rootf/f1/subf1
, children=[Folder [name=text1.txt, type=file, path=Parsing/rootf/folder1/subf1/text1.txt
, children=null, file=[a, b, c]]], file=null], Folder [name=subf2, type=folder, path=Parsing/rootf/f1/subf2
, children=null, file=[]], Folder [name=text2.txt, type=file, path=TParsing/rootf/f1/text2.txt
, children=null, file=[d, e, f]]], file=null], Folder [name=text1.txt, type=file, path=Parsing/rootd/text1.txt
, children=null, file=[aa, bb]]], file=null]

现在你有了一个层次结构,即根目录下的系统,其中包含文件夹和文件。

关于java - 将 JSON 对象映射到 Java 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56247252/

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