使用 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 对象。我已经从提供的 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;
}
}
我知道问题在于杰克逊正在锁定一个名为 LEI 的 Java 对象,并带有一个名为“$”的变量。但是没有。该组织的帮助服务说:
““$”对象总是复制相应 XML 元素的简单内容(即不是属性、子节点等)。“$”对象应始终键入为适用的 JSON 字符串。”
但据我了解,这不是 JSON 标准。
我的问题是:有没有办法让杰克逊将其解析为 LEI =“549300Q82NZ9NYNMZT63”等,而不是使用变量“$”来对象 LEI?一天的大部分时间都被困在这个问题上。
@UPDATE 根据客户服务,这种 JSON 格式显然被称为“BadgerFish 约定”。