我如何使用pojo class将无效的json解析为java对象

发布于 2025-02-03 17:53:51 字数 207 浏览 2 评论 0原文

我不确定此对象的格式是什么格式,但是我可以将以下无效的JSON对象解析为Java类POJO吗?我尝试使用杰克逊这样做,但是由于它无效,所以我想知道Pojo课程是否有效?

{ 
    name: (sindey, crosby)
    game: "Hockey"
    type: athlete
}

该文件将具有此格式的多个对象

I'm not sure what format this object is in but can I parse the following invalid JSON object to Java class Pojo? I tried doing it using Jackson but since it's invalid, I was wondering if pojo class would work?

{ 
    name: (sindey, crosby)
    game: "Hockey"
    type: athlete
}

The file would have multiple objects of this format

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花开半夏魅人心 2025-02-10 17:53:51

Geesh,不要识别这种格式!如果您想使用杰克逊 => $ 1:“ $ 2”

我想知道Pojo课程是否有效?

当然,您可以通过简单的解析器来完成该工作。有足够的改进空间,但是类似的事情可以做到:

    Record record = null;
    var records = new LinkedList<>();
    // I'm using a reader as example, but just provide lines any way you like
    while ((line = reader.readLine()) != null) {
        line = line.trim();
        // may need to skip empty lines if you have them in your file...
        if (line.equals("{")) {
            record = new Record();
            records.add(record);
        } else {
            // may need substrings if your data contains ":"
            var tokens = line.split(":");
            var field = tokens[0];
            var value = tokens[1];
            if (field.equals("name")) {
                // perhaps shuffle the format to something nicer here..
                record.setName(value);
            }
            /// same for game and type fields...
        }
    }

Geesh, don't recognise this format! If you want to use Jackson you could pre-process you data to wrap the values... perhaps a regex to catpure the groups and wrap the values in quotes something like (name|type):\s(.+) => $1: "$2"

I was wondering if pojo class would work?

Sure, you could make that work with a simple parser; plenty of room for improvement, but something like this would do it:

    Record record = null;
    var records = new LinkedList<>();
    // I'm using a reader as example, but just provide lines any way you like
    while ((line = reader.readLine()) != null) {
        line = line.trim();
        // may need to skip empty lines if you have them in your file...
        if (line.equals("{")) {
            record = new Record();
            records.add(record);
        } else {
            // may need substrings if your data contains ":"
            var tokens = line.split(":");
            var field = tokens[0];
            var value = tokens[1];
            if (field.equals("name")) {
                // perhaps shuffle the format to something nicer here..
                record.setName(value);
            }
            /// same for game and type fields...
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文