我正在尝试将JSON(myJson.json)对象转换为Java类。
使用以下How to convert Java Object to From Json Jackson @Mykong代码。
//2. Convert JSON to Java object
ObjectMapper mapper = new ObjectMapper();
StudentInfoDTO student = mapper.readValue(new File("c:\\myJson.json"), StudentInfoDTO.class);
但上述ObjectMapper
为unable to map the Parent(StudentDTO) Class Properties(loggedInUser,createdBy,...).
是否还有@annotations
可以映射父类属性。
欢迎任何建议。
我有以下内容 - StudentInfoDTO,AddressDTO,StudentDTO,SkillsDTO和myJson.json。
StudentInfoDTO:
public class StudentInfoDTO extends StudentDTO implements Serializable{
private static final long serialVersionUID = 1L;
private AddressDTO address;
private String studentName;
....getters and setters
}
AddressDTO:
public class AddressDTO implements Serializable{
private static final long serialVersionUID = 1L;
private String street;
private String city;
private String state;
private String country;
private Long zipcode;
....getters and setters
}
StudentDTO:
public class StudentDTO implements Serializable{
private static final long serialVersionUID = 1L;
private String loggedInUser;
private String createdBy;
private Date createdTs;
private String updatedBy;
private Date updatedTs;
private List<skillsDTO> skills;
....getters and setters
}
myJson.json
{
"address":
{
"street": "Hunterfly Place",
"city": "Dunlo",
"state": "Arizona",
"country": "Colorado",
"zipcode": "1631",
},
"studentName": "John",
"loggedInUser": "Admin",
"createdBy": "System",
"createdTs": "03-19-2011 14:53",
"updatedBy": "John",
"updatedTs": "06-11-2014 08:00",
"skills": [
{
"skill": "Football",
"grade": "A"
},
{
"skill": "Swimming",
"grade": "C"
},
{
"skill": "Dance",
"grade": "A"
}]
}
我的错误日志:
03:29:03,132 ERROR [stderr] (http-localhost-127.0.0.1-8080-2) org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "loggedInUser" (Class com.mySchool.project.services.StudentInfoDTO), not marked as ignorable
03:29:03,133 ERROR [stderr] (http-localhost-127.0.0.1-8080-2) at [Source: c:\myJson.json; line: 51, column: 23] (through reference chain: com.mySchool.project.services.StudentInfoDTO["address"]->com.mySchool.project.services.StudentInfoDTO["loggedInUser"])
03:29:03,133 ERROR [stderr] (http-localhost-127.0.0.1-8080-2) at org.codehaus.jackson.map.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:53)
03:29:03,134 ERROR [stderr] (http-localhost-127.0.0.1-8080-2) at org.codehaus.jackson.map.deser.StdDeserializationContext.unknownFieldException(StdDeserializationContext.java:246)
答案 0 :(得分:0)
基本上您正在尝试从StudentInfoDTO读取,但此类没有关于StudentDTO的信息。您需要更改一些代码(添加学生参数):
public class StudentInfoDTO implements Serializable{
private static final long serialVersionUID = 1L;
private AddressDTO address;
private StudentDTO student;
....getters and setters
}
你的JSON应该是这样的:
{
"address":
{
"street": "Hunterfly Place",
"city": "Dunlo",
"state": "Arizona",
"country": "Colorado",
"zipcode": "1631",
},
"student":
{
"studentName": "John",
"loggedInUser": "Admin",
"createdBy": "System",
"createdTs": "03-19-2011 14:53",
"updatedBy": "John",
"updatedTs": "06-11-2014 08:00",
"skills": [
{
"skill": "Football",
"grade": "A"
}
...
}
}
请注意,我在StudentDTO课程中添加了学生姓名。但这个想法仍然是一样的。您需要在JSON和类中具有相同的结构。
如果您无法更改JSON输出,只需更改类结构即可。例如,StudentInfoDTO具有“studentName”属性,并且还可能包括同一级别中的所有其他属性,如“loggedInUser”。您的解析器可能使用以下类:
public class StudentInfoDTO implements Serializable{
private static final long serialVersionUID = 1L;
private AddressDTO address;
private String studentName;
private String loggedInUser;
private String createdBy;
private Date createdTs;
private String updatedBy;
private Date updatedTs;
private List<skillsDTO> skills;
...
}
使用最后一种方法,您不需要StudentDTO课程。
如果您想使用扩展程序,那么您可能会更改超类的可见性:
public class StudentDTO implements Serializable{
private static final long serialVersionUID = 1L;
protected String loggedInUser;
protected String createdBy;
protected Date createdTs;
protected String updatedBy;
protected Date updatedTs;
protected List<skillsDTO> skills;
public StudentDTO () {}
....getters and setters
}
答案 1 :(得分:0)
抱歉方便。
它按预期工作。
我的myJson.json
文件出错了,我按照@Vishvesh Phadnis
{
"address":
{
"street": "Hunterfly Place",
"city": "Dunlo",
"state": "Arizona",
"country": "Colorado",
"zipcode": "1631"
},
"studentName": "John",
"loggedInUser": "Admin",
"createdBy": "System",
"createdTs": "03-19-2011 14:53",
"updatedBy": "John",
"updatedTs": "06-11-2014 08:00",
"skills": [
{
"skill": "Football",
"grade": "A"
},
{
"skill": "Swimming",
"grade": "C"
},
{
"skill": "Dance",
"grade": "A"
}]
}
全部谢谢:)