使用这些:
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
我有一个文件,里面有两个YAML结构
version: 1.0
time:
day: 20
second: 15
minute: 39
hour: 19
weekday: 4
month: 1
year: 1999
ironman: true
rulesets:
- Xcom1Ruleset
- XcomUtil_Always_Daytime
- XcomUtil_Skyranger_Weapon_Slot
- XcomUtil_Starting_Defensive_Improved_Base
- XcomUtil_Pistol_Auto_Shot
---
alienStrategy:
(...)
我试过这个:
directives = mapper.readValue(fileIn, Directives.class);
savegame = mapper.readValue(fileIn, Savegame.class);
但是我收到了这个例外:
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "name" (class app.Savegame), not marked as ignorable (24 known properties: "globeZoom", "bases", "monthsPassed", "warned", "discovered", "incomes", "rng", "funds", "globeLat", "maintenance", "expenditures", "ufos", "graphCountryToggles", "ids", "alienStrategy", "difficulty", "researchScores", "graphRegionToggles", "globeLon", "regions", "alienMissions", "graphFinanceToggles", "countries", "deadSoldiers"])
at [Source: C:\Users\Luis\Desktop\in.txt; line: 1, column: 12] (through reference chain: app.Savegame["name"])
at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:62)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:833)
at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1096)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1467)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1445)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:282)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:140)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3789)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2731)
at app.Main.main(Main.java:34)
它映射的Directive.java组件好了,但是Savegame.java(带有alienStrategy和更多属性)试图映射,它失败了。
请随时编辑此问题,以便向其他用户说明。