将JSON转换为Java Object,如何使用Jackson解析BadgerFish约定

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将JSON转换为Java Object,如何使用Jackson解析BadgerFish约定相关的知识,希望对你有一定的参考价值。

使用API​​我收到这样的JSON(现在保存到文件中):

[{
    "LEI": {
        "$": "549300Q82NZ9NYNMZT63"
    },
    "Entity": {
        "LegalName": {
            "$": "United Nerds in Collaboration of Random Nerdiness AB"
        },
        "LegalAddress": {
            "Line1": {
                "$": "BOX 155"
            },
            "City": {
                "$": "Alingsas"
            },
            "Region": {
                "$": "SE-O"
            },
            "Country": {
                "$": "SE"
            },
            "PostalCode": {
                "$": "44123"
            }
        },
        "HeadquartersAddress": {
            "Line1": {
                "$": "BOX 155"
            },
            "City": {
                "$": "Alingsas"
            },
            "Region": {
                "$": "SE-O"
            },
            "Country": {
                "$": "SE"
            },
            "PostalCode": {
                "$": "44123"
            }
        },
        "BusinessRegisterEntityID": {
            "@register": "SE001",
            "$": "5568557184"
        },
        "LegalJurisdiction": {
            "$": "SE"
        },
        "LegalForm": {
            "$": "PRIVATA AKTIEBOLAG"
        },
        "EntityStatus": {
            "$": "ACTIVE"
        }
    },
    "Registration": {
        "InitialRegistrationDate": {
            "$": "2016-06-23T01:48:45.025Z"
        },
        "LastUpdateDate": {
            "$": "2016-06-23T01:48:44.945Z"
        },
        "RegistrationStatus": {
            "$": "ISSUED"
        },
        "NextRenewalDate": {
            "$": "2017-06-21T06:32:03.821Z"
        },
        "ManagingLOU": {
            "$": "EVK05KS7XY1DEII3R011"
        },
        "ValidationSources": {
            "$": "PARTIALLY_CORROBORATED"
        }
    }
}]

我想从这些中获取Java Object。我已经从提供的xsd文件中创建了Java对象。我正在运行的代码是:

public static void toJava() {
    ObjectMapper mapper = new ObjectMapper();
    try {
        File json = new File("C:\temp\JSON.json");
        LEIRecordType[] type = mapper.readValue(json, LEIRecordType[].class);
    } catch (JsonEOFException ex) {
        ex.printStackTrace();
    } catch (JsonMappingException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

这会创建以下异常:

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "LEI" (class   org.leiroc.data.schema.leidata._2014.LEIRecordType), not marked as ignorable (5 known properties: "lei", "registration", "entity", "nextVersion", "extension"])
 at [Source: (File); line: 3, column: 14] (through reference chain: java.lang.Object[][0]->org.leiroc.data.schema.leidata._2014.LEIRecordType["LEI"])
at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:60)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:822)
at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1152)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1567)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1545)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:293)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:195)
at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:21)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4001)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2890)
at Test.JSONParser.toJava(JSONParser.java:38)
at Test.JSONParser.main(JSONParser.java:29)

LEIRecordType如下所示:

package org.leiroc.data.schema.leidata._2014;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "LEIRecordType", propOrder = {"lei", "entity", "registration", "nextVersion", "extension"})
public class LEIRecordType {

    @XmlElement(name = "LEI", required = true)
    protected String lei;

    @XmlElement(name = "Entity", required = true)
    protected EntityType entity;

    @XmlElement(name = "Registration", required = true)
    protected RegistrationType registration;

    @XmlElement(name = "NextVersion")
    protected LEIRecordNextVersionType nextVersion;

    @XmlElement(name = "Extension")
    protected ExtensionType extension;

    public String getLEI() {
        return this.lei;
    }

    public void setLEI(String paramString) {
        this.lei = paramString;
    }

    public EntityType getEntity() {
        return this.entity;
    }

    public void setEntity(EntityType paramEntityType) {
        this.entity = paramEntityType;
    }

    public RegistrationType getRegistration() {
        return this.registration;
    }

    public void setRegistration(RegistrationType paramRegistrationType) {
        this.registration = paramRegistrationType;
    }

    public LEIRecordNextVersionType getNextVersion() {
        return this.nextVersion;
    }

    public void setNextVersion(LEIRecordNextVersionType paramLEIRecordNextVersionType) {
        this.nextVersion = paramLEIRecordNextVersionType;
    }

    public ExtensionType getExtension() {
        return this.extension;
    }

    public void setExtension(ExtensionType paramExtensionType) {
        this.extension = paramExtensionType;
    }
}

据我所知,问题是jackson正在锁定一个名为LEI的Java对象,其变量名为“$”。但没有。组织帮助服务说:

“$”对象总是重现相应XML元素的简单内容(即不是属性,子节点等)。“$”对象应始终在适用的地方键入JSON字符串。“

但据我所知,这不是JSON标准。

我的问题是:有没有办法让杰克逊解析这个LEI =“549300Q82NZ9NYNMZT63”等而不是和对象LEI的变量“$”?一天中的大部分时间都被困在这上面。

@UPDATE根据客户服务,这种JSON格式显然被称为“BadgerFish惯例”。

答案

由于$对象始终是String,因此您可以为处理BadgerFish包装器对象的Strings创建自定义反序列化器。

此反序列化程序检查String值周围是否存在BadgerFish包装器对象并将其解包。正常的String值像往常一样被反序列化。

public class BadgerFishDeserializer extends StdDeserializer<String> {

    private static final long serialVersionUID = 1L;

    private static final SerializedString BADGER_FISH_FIELD_NAME = new SerializedString("$");

    public BadgerFishDeserializer() {
        super(String.class);
    }

    @Override
    public String deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        // do we have a wrapper object?
        if (jp.isExpectedStartObjectToken()) {          
            // check if first field name is equal to '$'
            if (!jp.nextFieldName(BADGER_FISH_FIELD_NAME)) {
                ctxt.reportInputMismatch(String.class, "Expected BadgerFish field name '$', but got '%s'", jp.getCurrentName());
            }
            jp.nextValue();  // proceed to the actual value
            String value = jp.getValueAsString();  // read value as string
            jp.nextToken();  // consume END_OBJECT of wrapper object
            return value;
        }
        // else: just return string value
        return jp.getValueAsString();
    }

}

最后在Jackson ObjectMapper实例上注册模块:

SimpleModule module = new SimpleModule();
module.addDeserializer(String.class, new BadgerFishDeserializer());
mapper.registerModule(module);

注意:如果您只想要解包某些属性,可以创建自定义注释并使用BeanDeserializerModifier检查注释,然后提供处理包装器对象的反序列化器。

一些值得思考的东西:

  • 创建注释
  • 修改反序列化器以始终指望包装器对象(在纯字符串上失败)
  • 创建一个DeserializerModifier
  • DeserializerModifier上注册ObjectMapper

困难的部分:

public class BadgerFishDeserializerModifier extends BeanDeserializerModifier {

    @Override
    public BeanDeserializerBuilder updateBuilder(DeserializationConfig config, BeanDescription beanDesc, BeanDeserializerBuilder builder) {
        Iterator<SettableBeanProperty> props = builder.getProperties();
        while (props.hasNext()) {
            SettableBeanProperty prop = props.next();
            if (prop.getAnnotation(MyAnnotation.class) != null) {   
                builder.addOrReplaceProperty(prop.withValueDeserializer(new BadgerFishDeserializer()), true);
            }
        }
        return builder;
    }

}
另一答案

这非常有帮助!我最终不得不为String和XMLGregorianCalendar做一个特殊的反序列化器。但问题并不止于此。 BadgerFish从生成的类中获取@XMLAttributes并将它们设置为@value而不是“value”。例如:

"BusinessRegisterEntityID": {
        "@register": "SE001",
        "$": "5568557184"
    }

那么他们的任何方式也可以将字段名称自定义回原始名称?目前我现在得到这个例外:

Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "@register" (class leigen.BusinessRegisterEntityIDType), not marked as ignorable (2 known properties: "value", "register"])
at [Source: (File); line: 44, column: 27] (through reference chain: java.lang.Object[][0]->leigen.LEIRecordType["Entity"]->leigen.EntityType["BusinessRegisterEntityID"]->leigen.BusinessRegisterEntityIDType["@register"])
at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:60)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:822)
at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1152)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1567)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1545)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:293)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:136)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:287)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:136)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:287)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:195)
at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:21)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4001)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2890)
at Test.JSONParser.toJava(JSONParser.java:47)
at Test.JSONParser.main(JSONParser.java:28)

我可以通过在配置中添加“ignore”(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)来绕过这个,就像我现在的代码一样:

    public static LEIRecordType[] toJava(File json) throws JsonParseException, JsonMappingException, IOException {
    ObjectMapper mapper = new ObjectMapper();
    SimpleModule module = new SimpleModule();
    module.addDeserializer(String.class, new BadgerFishStringDeserializer());
    module.addDeserializer(XMLGregorianCalendar.class, new BadgerFishXMLGregorianCalendarDeserializer());
    module.addDeserializer(NameType.class, new BadgerFishNameTypeDeserializer());
    mapper.registerModule(module);
    mapper.registerModule(new JaxbAnnotationModule()); // To be able to read JAXB annotations.

    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    return mapper.readValue(json, LEIRecordType[].class);
}   

但是这只会将以@开头的值设置为null。还有另一种解决方法吗?否则,我将不得不为我生成的所有类编写一个自定义反序列化器,大约25个(在下一个版本中可能总会有更多)。我已经为NameType做了一个,但是对于其他示例,还需要更多。

以上是关于将JSON转换为Java Object,如何使用Jackson解析BadgerFish约定的主要内容,如果未能解决你的问题,请参考以下文章

如何把一个json文件转换成字符串

如何在播放框架 Java 中将 List<Object> 转换为 JSON

如何使用java和jsp和extjs将java arraylist转换为json数组存储数据

java如何将json的数据转换为map或者list类型的?

java 怎么将对象转换成json字符串

Java中如何把字符串转换成map