我正在尝试解析一个看起来像这样的yaml文件(它是动态的,所以这只是一个例子):
size: 4533
vehicles:
'1':
vecUUID: fa461669-6b88-418d-8e7c-219e784a21fc
ownerUUID: da36067f-7af8-411b-b820-8e0709b4d64e
invString: |
rO0ABXcEAAAACXBwcHBwcHBwcA==
'2':
vecUUID: 3c192337-c9dd-45f0-92c0-545d42d5404d
ownerUUID: da36067f-7af8-411b-b820-8e0709b4d64e
invString: |
rO0ABXcEAAAACXBwcHBwcHBwcA==
'3':
vecUUID: d5df362b-c4f8-4d82-9e3a-c37fba1162f0
ownerUUID: fd44ce79-32b5-4080-9953-3d0f4d5399b1
invString: |
rO0ABXcEAAAAEnBwcHBwcHBwc3IAGm9yZy5idWtraXQudXRpbC5pby5XcmFwcGVy8lBH7PESbwUC
AAFMAANtYXB0AA9MamF2YS91dGlsL01hcDt4cHNyADVjb20uZ29vZ2xlLmNvbW1vbi5jb2xsZWN0
LkltbXV0YWJsZU1hcCRTZXJpYWxpemVkRm9ybQAAAAAAAAAAAgACWwAEa2V5c3QAE1tMamF2YS9s
YW5nL09iamVjdDtbAAZ2YWx1ZXNxAH4ABHhwdXIAE1tMamF2YS5sYW5nLk9iamVjdDuQzlifEHMp
bAIAAHhwAAAAA3QAAj09dAAEdHlwZXQABmFtb3VudHVxAH4ABgAAAAN0AB5vcmcuYnVra2l0Lmlu
dmVudG9yeS5JdGVtU3RhY2t0AAxHTEFTU19CT1RUTEVzcgARamF2YS5sYW5nLkludGVnZXIS4qCk
94GHOAIAAUkABXZhbHVleHIAEGphdmEubGFuZy5OdW1iZXKGrJUdC5TgiwIAAHhwAAAADHBwcHBw
cHBwcA==
我创建了3个类,如下所示。Vehicle,VehicleInfo和MyYamlFile。
车辆
public class Vehicle {
@JsonProperty
private List<VehicleInfo> info;
// getter methods ommitted
}
车辆信息
public class VehicleInfo {
@JsonProperty
private String vecUUID;
@JsonProperty
private String ownerUUID;
@JsonProperty
private String invString;
// getter methods ommitted
}
MyYamlFile
public class MyYamlFile {
@JsonProperty
private List<Vehicle> vehicles;
@JsonProperty
private int size;
// getter methods ommitted
}
问题是当我运行它时,如下所示:
public class Fix {
public static void main(String[] args) {
MyYamlFile file = readYaml(new File("E:\\dev\\FrihedenBilInventoryFix\\VehicleInventory.yaml"));
System.out.print("test: " + file.getSize());
}
public static MyYamlFile readYaml(final File file) {
final ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); // jackson databind
try {
return mapper.readValue(file, MyYamlFile.class);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
我遇到错误
com.fasterxml.jackson.databind.exc.MismatchedInputException:无法从START_OBJECT令牌中反序列化java.util.ArrayList实例 在[来源:(文件);行:3,列:3](通过参考链:MyYamlFile [“ vehicles”])
知道为什么吗?
答案 0 :(得分:1)
您正在尝试将文件解析为List
。
列表的所有成员都是以“-”(破折号和空格)开头的相同缩进级别的行:
在您的情况下,所有应为列表成员的项目的格式都为'1':
,这是不正确的。