将JSON对象映射到Java类

时间:2019-05-21 22:52:30

标签: java json gson

我有一个解析为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的{​​{1}}包含文件的内容。

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

我最初尝试使用GSON将其映射到三个类fileSystem.javaFolder.java,它们包含File.javaprivate List<Folder> children和{{ 1}}。仅当我具有特定的JSON结构(RootFolder-> Sub-Folder-> File)时,此方法才有效。有什么方法可以使映射更笼统,以便包括private List<File> children可以包含private List<String> childrenSystem的条件,并且类似地,Folder可以包含{ {1}}和File

2 个答案:

答案 0 :(得分:0)

如果仅将Folder对象扩展为同时具有private List<Folder> subfoldersprivate List<File> files,并将System对象设为特殊的Folder对象,则您的原始想法似乎很好。这将使您能够表示数据的布局以及类似内容。唯一的限制是,只有文件夹可以包含文件,而其他文件夹可以作为子项,文件只能包含字符串作为子项。

答案 1 :(得分:0)

在JSON中有一个陷阱,即如果内容是文件或文件夹,则将其表示为children,因此,我尝试了一种方法,将children JSON密钥替换为{{1} },如果它使用正则表达式在其中没有任何JSON对象的情况下收集了字符串。完成后,我只需要一个类,即我的文件夹将其解析为JSON到Java类。
所以我的课如下。

file

用于转换密钥然后将其解析为此类的代码

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 + "]";
    }
}

输出

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);

    }

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