我有以下JSON数据库:
{"houses":[{"doors":[{"state":false,"requests":[]}],"users":[{"username":"blucas","password":"Phantom1"}],"passcode":"1324"},null,null,null,null,null,null,null,null,null]}
我使用此代码生成的:
File database = new File("database.json");
Door door1 = new Door();
ArrayList<Door> doors = new ArrayList<Door>();
doors.add(door1);
User blucas = new User("blucas", "Phantom1");
ArrayList<User> users= new ArrayList<User>();
users.add(blucas);
House house1 = new House(doors, users);
ArrayList<House> houseList = new ArrayList<House>();
houseList.add(house1);
Houses houses = new Houses(houseList);
objMapper.writeValue(database, houses);
当我尝试以最简单的方式访问数据库时:
byte[] jsonData = null;
try {
jsonData = Files.readAllBytes(Paths.get("database.json"));
} catch (IOException e) {
e.printStackTrace();
}
ObjectMapper objectMapper = new ObjectMapper();
try {
houses = objectMapper.readValue(jsonData, Houses.class);
} catch (IOException e) {
e.printStackTrace();
}
House[] myHouses = houses.getHouses();
House house1 = myHouses[0];
System.out.println(house1.checkPasscode("1324"));
System.exit(1);
我收到错误:
com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of Server.House: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?)
at [Source: database.json; line: 1, column: 13] (through reference chain: Server.Houses["houses"]->java.lang.Object[][0])
Door,House,Users和Houses都是用户定义的类,它们与生成数据库的代码以及尝试打开和处理数据库的代码一起存在于同一个包中。
我已经彻底查看了这个问题的解决方案,但我似乎无法找到为什么我会收到此错误,我对JSON和数据库都很陌生,因此非常感谢任何建议。
答案 0 :(得分:1)
在House类中没有默认构造函数。在House类中添加默认构造函数,不带参数。