当前位置: 首页 > 知识库问答 >
问题:

ObjectMapper类构造

翁宜年
2023-03-14

我有大量的数据,我想让它成为java中的一个对象(例如。https://haste.razvancode.com/agiyamuyol.json)

我正在运行以下代码:

        ObjectMapper mapper = new ObjectMapper();
        
        File f = new File("example.json");
        if (!f.exists()) f.createNewFile();

        Board board = mapper.readValue(f, Board.class);

        System.out.println(board.getName());

我得到了这个错误:

Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "isTemplate" (class com.razvancode.discordbot.Utils.Board$Prefs), not marked as ignorable (21 known properties: "calendarFeedEnabled", "voting", "backgroundBottomColor", "cardAging", "backgroundImage", "background", "canBePrivate", "canBeOrg", "comments", "permissionLevel", "selfJoin", "canInvite", "invitations", "backgroundTopColor", "backgroundBrightness", "hideVotes", "cardCovers", "canBeEnterprise", "backgroundTile", "canBePublic", "backgroundImageScaled"])
 at [Source: (File); line: 35, column: 23] (through reference chain: com.razvancode.discordbot.Utils.Board["prefs"]->com.razvancode.discordbot.Utils.Board$Prefs["isTemplate"])
    at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:61)
    at com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:823)
    at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1153)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1589)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1567)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:258)
    at com.fasterxml.jackson.databind.deser.impl.InnerClassProperty.deserializeAndSet(InnerClassProperty.java:90)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:288)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2902)
    at com.razvancode.discordbot.Test.<init>(Test.java:28)
    at com.razvancode.discordbot.Test.main(Test.java:34)

Process finished with exit code 1

我百分之百确定那是我的董事会课程,但是我现在已经工作了几个小时,我仍然不能让它工作。

董事会级别:

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.ArrayList;

@NoArgsConstructor
@AllArgsConstructor
public class Board {

    @Getter
    private Object descData, pinned, datePluginDisable, idBoardSource, limits, templateGallery, ixUpdate, idEnterprise, idMemberCreator;
    @Getter
    private String shortUrl, dateLastActivity, shortLink, creationMethod, idOrganization, dateLastView, id, url, name, desc;
    @Getter
    private boolean subscribed, starred, enterpriseOwned, closed;
    @Getter
    private ArrayList<Memberships> memberships;
    @Getter
    private ArrayList<String> idTags, powerUps, premiumFeatures;
    @Getter
    private LabelNames labelNames;
    @Getter
    private Prefs prefs;

    @NoArgsConstructor
    @AllArgsConstructor
    public static class LabelNames {

        @Getter
        private String orange, red, sky, pink, green, blue, lime, yellow, black, purple;
    }

    @NoArgsConstructor
    @AllArgsConstructor
    public static class Prefs {

        @Getter
        private String backgroundBrightness, comments, backgroundTopColor, backgroundImage, backgroundBottomColor, voting, permissionLevel, cardAging, invitations, background;
        @Getter
        private boolean canBeEnterprise, hideVotes, canBeOrg, calendarFeedEnabled, backgroundTile, canBePublic, canBePrivate, canInvite, isTemplate, cardCovers, selfJoin;
        @Getter
        private ArrayList<BackgroundImageScaled> backgroundImageScaled;
    }

    @NoArgsConstructor
    @AllArgsConstructor
    public static class BackgroundImageScaled {

        @Getter
        private String url;
        @Getter
        private Long width, height;
    }

    @NoArgsConstructor
    @AllArgsConstructor
    public static class Memberships {

        @Getter
        private String idMember, id, memberType;
        @Getter
        private boolean unconfirmed, deactivated;
    }
}

如果你对我如何修复它有任何想法,或者我错在哪里,请告诉我。

共有1个答案

笪欣嘉
2023-03-14

这可能与这个答案有关。您可能需要阻止lombok将getter生成为isTemplate()而不是ISistTemplate(),因为jackson将假定数据中的布尔字段称为template。

 类似资料:
  • 介绍 (Introduction) ObjectMapper是Jackson库的主要actor类。 ObjectMapper类ObjectMapper提供了从基本POJO(普通旧Java对象)或从通用JSON树模型(JsonNode)读取和写入JSON的功能,以及执行转换的相关功能。 它还可以高度自定义,可以使用不同样式的JSON内容,并支持更高级的Object概念,如多态和对象标识。 Objec

  • 主要内容:1 ObjectMapper类的构造方法,2 ObjectMapper类的示例ObjectMapper是Jackson库的核心类。ObjectMapper提供用于在基本POJO(Java对象)之间或从通用JSON树模型(JsonNode)之间读取和写入JSON的功能,以及用于执行转换的相关功能。它也可以高度自定义,使用不同样式的JSON输出,并支持更高级的Object概念,例如多态。ObjectMapper还充当更高级ObjectReader和ObjectWriter类的工

  • ObjectMapper 是使用 Swift 编写的框架,能帮助你转换 Model 对象成为 JSON,反之亦然。

  • 问题内容: 我正在使用Jackson示例代码对POJO进行反序列化: 这行抛出一个NoSuchMethodError: 我不明白 问题答案: 我猜您的Jackson JAR不同步。本类是JAR,和类是在。 确保它们都是相同的版本。

  • ObjectMapper + RealmSwift 空值校验 需求, 在通过ObjectMapper解析时, 若value不满足条件整个对象返回nil 演示demo: import Foundation import ObjectMapper import RealmSwift class TestFModel: Object, Mappable { @objc dynamic var

  • ObjectMapper .NET 是一个 .NET 平台的关系对象映射框架。