我是一名研究生,正在尝试学习如何使用杰克逊将json解析为对象。
I'm currently trying to parse this
{
"EmptyMessage":"",
"EmptyMessageType":0,
"Result":[
{
"HasEducationPlan":false,
"MembersUrl":"https://student.ulife.com.br/DisciplineMembersList/Index?cd=zzlaAJL5JngfbcsZdA37nw%3d%3d",
"CalendarUrl":"https://student.ulife.com.br/Calendar?cd=zzlaAJL5JngfbcsZdA37nw%3d%3d",
"EvaluationUrl":"https://www.ulife.com.br/Student/PAT/Grades.aspx?cd=zzlaAJL5JngfbcsZdA37nw%3d%3d",
"DiscussionUrl":"https://www.ulife.com.br/Student/PAT/DiscussionForum.aspx?cld=zzlaAJL5JngfbcsZdA37nw%3d%3d",
"ContentUrl":"https://student.ulife.com.br/Lesson/Index?cd=zzlaAJL5JngfbcsZdA37nw%3d%3d",
"GradeUrl":"https://student.ulife.com.br/Grade/Index?cd=zzlaAJL5JngfbcsZdA37nw%3d%3d&st=ACFdBAwNYJcCXLzBpH3P2A%3d%3d",
"CourseUrl":"https://www.ulife.com.br/OrganizationCourse/OrganizationCourseDetail.aspx?o=NaltKyU6OKV8RcmQsxNuow%3d%3d&c=pfRucFwwUMlY%2fZLlwzIjUA%3d%3d",
"ClassUrl":"/classes/classpage.aspx?ClassId=HxpJ4cUZ4IMKkDlj0kHcGg%3d%3d",
"PerformanceUrl":null,
"FrequencyUrl":"/Frequency/ClassDisciplineFrequency.aspx?d=nY1NINmbyIbvqz57uw6Opw%3d%3d&cn=qOJe4AcqjlNHYLakMKbXN9xDtpV0kzjJS%2bcMI7I5%2bEA%3d",
"DisciplineUrl":"/PAT/Discipline.aspx?p=VowhoBFwb8B%2b9wSJMxKnGVSJJ%2bjEks6OW%2fm%2bxzwPHeg%3d",
"ChallengeUrl":"https://www.ulife.com.br/Controls/EnadeChallenge.aspx?scd=22405329",
"ViewMembers":true,
"NewFeatureEnabled":true,
"StudentClassDisciplineID":22405329,
"StudentID":0,
"ClassDisciplineID":710835,
"StudentClassDisciplineStatus":1,
"DisciplineContentID":610112,
"HasDisciplineContent":true,
"CurriculumEducationPlanID":0,
"OrganizationID":2418,
"DisciplineID":336026,
"DisciplineName":"Cálculo Diferencial",
"IsMultiOrganization":false,
"Enrolled":true,
"ClassID":586235,
"ClassName":"MAT2AN-CMA-740642",
"GradeID":3421,
"GradeName":"Graduação (Teórica)",
"OrganizationCourseID":35873,
"OrganizationCourseName":"Matemática",
"OriginOrganizationCourseID":0,
"ModuleStepID":246695,
"ModuleStepName":"UNIBH_2019/1",
"ModuleStepStartDate":"2019-01-01T00:00:00",
"ModuleStepEndDate":"2019-07-31T00:00:00",
"HasFrequency":false,
"HasEvaluationPeriod":false,
"HasEnade":false,
"StudentActive":true,
"EnadeChallengeTypeId":0,
"HasDiscussionForum":false,
"NewLessonCategoryCount":0,
"Year":0,
"DisciplineContentCoverImageUrl":"",
"LessonCount":2
},
{
"HasEducationPlan":false,
"MembersUrl":"https://student.ulife.com.br/DisciplineMembersList/Index?cd=0%2fxd%2bn56aFe%2fgp2xBqXe0Q%3d%3d",
"CalendarUrl":"https://student.ulife.com.br/Calendar?cd=0%2fxd%2bn56aFe%2fgp2xBqXe0Q%3d%3d",
"EvaluationUrl":"https://www.ulife.com.br/Student/PAT/Grades.aspx?cd=0%2fxd%2bn56aFe%2fgp2xBqXe0Q%3d%3d",
"DiscussionUrl":"https://www.ulife.com.br/Student/PAT/DiscussionForum.aspx?cld=0%2fxd%2bn56aFe%2fgp2xBqXe0Q%3d%3d",
"ContentUrl":"https://student.ulife.com.br/Lesson/Index?cd=0%2fxd%2bn56aFe%2fgp2xBqXe0Q%3d%3d",
"GradeUrl":"https://student.ulife.com.br/Grade/Index?cd=0%2fxd%2bn56aFe%2fgp2xBqXe0Q%3d%3d&st=ACFdBAwNYJcCXLzBpH3P2A%3d%3d",
"CourseUrl":"https://www.ulife.com.br/OrganizationCourse/OrganizationCourseDetail.aspx?o=NaltKyU6OKV8RcmQsxNuow%3d%3d&c=P56mWzUmVubJFqSYQZZBTA%3d%3d",
"ClassUrl":"/classes/classpage.aspx?ClassId=ToF93aLWQkJzIIFV0b3mSw%3d%3d",
"PerformanceUrl":null,
"FrequencyUrl":"/Frequency/ClassDisciplineFrequency.aspx?d=JYmXKwR4RW0MX%2bR8Kwys%2fQ%3d%3d&cn=w1DRd2w9Q%2fcMoXjZ3pLs6D5hDrzbwNfMwAuvQmPULHM%3d",
"DisciplineUrl":"/PAT/Discipline.aspx?p=PL8%2f48nLqJNCG%2f99VuSqK3S5kwMhst8pjW83%2fJ673F4%3d",
"ChallengeUrl":"https://www.ulife.com.br/Controls/EnadeChallenge.aspx?scd=22406703",
"ViewMembers":true,
"NewFeatureEnabled":true,
"StudentClassDisciplineID":22406703,
"StudentID":0,
"ClassDisciplineID":711306,
"StudentClassDisciplineStatus":1,
"DisciplineContentID":0,
"HasDisciplineContent":false,
"CurriculumEducationPlanID":0,
"OrganizationID":2418,
"DisciplineID":345469,
"DisciplineName":"Física - Mecânica da Partícula",
"IsMultiOrganization":false,
"Enrolled":false,
"ClassID":586740,
"ClassName":"CCP1AN-CMA-740631",
"GradeID":3421,
"GradeName":"Graduação (Teórica)",
"OrganizationCourseID":36597,
"OrganizationCourseName":"Ciência da Computação",
"OriginOrganizationCourseID":0,
"ModuleStepID":246695,
"ModuleStepName":"UNIBH_2019/1",
"ModuleStepStartDate":"2019-01-01T00:00:00",
"ModuleStepEndDate":"2019-07-31T00:00:00",
"HasFrequency":false,
"HasEvaluationPeriod":false,
"HasEnade":false,
"StudentActive":true,
"EnadeChallengeTypeId":0,
"HasDiscussionForum":false,
"NewLessonCategoryCount":0,
"Year":0,
"DisciplineContentCoverImageUrl":"",
"LessonCount":0
},
{
"HasEducationPlan":false,
"MembersUrl":"https://student.ulife.com.br/DisciplineMembersList/Index?cd=CNqtVoDvLfuYHUOhGmzOQg%3d%3d",
"CalendarUrl":"https://student.ulife.com.br/Calendar?cd=CNqtVoDvLfuYHUOhGmzOQg%3d%3d",
"EvaluationUrl":"https://www.ulife.com.br/Student/PAT/Grades.aspx?cd=CNqtVoDvLfuYHUOhGmzOQg%3d%3d",
"DiscussionUrl":"https://www.ulife.com.br/Student/PAT/DiscussionForum.aspx?cld=CNqtVoDvLfuYHUOhGmzOQg%3d%3d",
"ContentUrl":"https://student.ulife.com.br/Lesson/Index?cd=CNqtVoDvLfuYHUOhGmzOQg%3d%3d",
"GradeUrl":"https://student.ulife.com.br/Grade/Index?cd=CNqtVoDvLfuYHUOhGmzOQg%3d%3d&st=ACFdBAwNYJcCXLzBpH3P2A%3d%3d",
"CourseUrl":"https://www.ulife.com.br/OrganizationCourse/OrganizationCourseDetail.aspx?o=NaltKyU6OKV8RcmQsxNuow%3d%3d&c=aYIoCh5SkcHin16ztI9Wcw%3d%3d",
"ClassUrl":"/classes/classpage.aspx?ClassId=V%2bsTc0p2Tb3aFsnKomueEg%3d%3d",
"PerformanceUrl":null,
"FrequencyUrl":"/Frequency/ClassDisciplineFrequency.aspx?d=RKhkP9%2fdtqRgJUTsCynP1A%3d%3d&cn=w1DRd2w9Q%2fcMoXjZ3pLs6G8uT7M0JST1CtE5q%2f3qds4%3d",
"DisciplineUrl":"/PAT/Discipline.aspx?p=RvH7tbqmb2d5Ti%2b34CzYYV44YcyWPZJIZVJggmJ2lug%3d",
"ChallengeUrl":"https://www.ulife.com.br/Controls/EnadeChallenge.aspx?scd=22399401",
"ViewMembers":true,
"NewFeatureEnabled":true,
"StudentClassDisciplineID":22399401,
"StudentID":0,
"ClassDisciplineID":711023,
"StudentClassDisciplineStatus":1,
"DisciplineContentID":609052,
"HasDisciplineContent":true,
"CurriculumEducationPlanID":0,
"OrganizationID":2418,
"DisciplineID":337155,
"DisciplineName":"Institucional I",
"IsMultiOrganization":true,
"Enrolled":true,
"ClassID":586433,
"ClassName":"CCP1AN-CMA-740638",
"GradeID":3401,
"GradeName":"Graduação",
"OrganizationCourseID":33548,
"OrganizationCourseName":"Vinte por cento",
"OriginOrganizationCourseID":0,
"ModuleStepID":246829,
"ModuleStepName":"UNIBH_2019/1",
"ModuleStepStartDate":"2019-01-01T00:00:00",
"ModuleStepEndDate":"2019-07-31T00:00:00",
"HasFrequency":false,
"HasEvaluationPeriod":false,
"HasEnade":false,
"StudentActive":true,
"EnadeChallengeTypeId":0,
"HasDiscussionForum":false,
"NewLessonCategoryCount":23,
"Year":0,
"DisciplineContentCoverImageUrl":"",
"LessonCount":17
},
{
"HasEducationPlan":false,
"MembersUrl":"https://student.ulife.com.br/DisciplineMembersList/Index?cd=MWRz4NHw1JSCymJKHyvoMQ%3d%3d",
"CalendarUrl":"https://student.ulife.com.br/Calendar?cd=MWRz4NHw1JSCymJKHyvoMQ%3d%3d",
"EvaluationUrl":"https://www.ulife.com.br/Student/PAT/Grades.aspx?cd=MWRz4NHw1JSCymJKHyvoMQ%3d%3d",
"DiscussionUrl":"https://www.ulife.com.br/Student/PAT/DiscussionForum.aspx?cld=MWRz4NHw1JSCymJKHyvoMQ%3d%3d",
"ContentUrl":"https://student.ulife.com.br/Lesson/Index?cd=MWRz4NHw1JSCymJKHyvoMQ%3d%3d",
"GradeUrl":"https://student.ulife.com.br/Grade/Index?cd=MWRz4NHw1JSCymJKHyvoMQ%3d%3d&st=ACFdBAwNYJcCXLzBpH3P2A%3d%3d",
"CourseUrl":"https://www.ulife.com.br/OrganizationCourse/OrganizationCourseDetail.aspx?o=NaltKyU6OKV8RcmQsxNuow%3d%3d&c=P56mWzUmVubJFqSYQZZBTA%3d%3d",
"ClassUrl":"/classes/classpage.aspx?ClassId=K2fUaz1lIjYVYZWVDbIesw%3d%3d",
"PerformanceUrl":null,
"FrequencyUrl":"/Frequency/ClassDisciplineFrequency.aspx?d=psxKtayBeShMNC%2fkhyMDRg%3d%3d&cn=fH8YHJoBctFFuBbe3vjGmPI3JKSMxWHK8TQxvSLpT60%3d",
"DisciplineUrl":"/PAT/Discipline.aspx?p=4D%2bP9RSBlvW5IEgbTzUVxhIucuZyACIN%2bmIa58PxyT4%3d",
"ChallengeUrl":"https://www.ulife.com.br/Controls/EnadeChallenge.aspx?scd=22403590",
"ViewMembers":true,
"NewFeatureEnabled":true,
"StudentClassDisciplineID":22403590,
"StudentID":0,
"ClassDisciplineID":711473,
"StudentClassDisciplineStatus":1,
"DisciplineContentID":611058,
"HasDisciplineContent":true,
"CurriculumEducationPlanID":0,
"OrganizationID":2418,
"DisciplineID":345470,
"DisciplineName":"Laboratório de Práticas Integradas 1A",
"IsMultiOrganization":false,
"Enrolled":true,
"ClassID":586925,
"ClassName":"CCP1AN-CMA1-740670",
"GradeID":3432,
"GradeName":"Graduação (Prática)",
"OrganizationCourseID":36597,
"OrganizationCourseName":"Ciência da Computação",
"OriginOrganizationCourseID":0,
"ModuleStepID":246695,
"ModuleStepName":"UNIBH_2019/1",
"ModuleStepStartDate":"2019-01-01T00:00:00",
"ModuleStepEndDate":"2019-07-31T00:00:00",
"HasFrequency":false,
"HasEvaluationPeriod":false,
"HasEnade":false,
"StudentActive":true,
"EnadeChallengeTypeId":0,
"HasDiscussionForum":false,
"NewLessonCategoryCount":0,
"Year":0,
"DisciplineContentCoverImageUrl":"",
"LessonCount":2
},
{
"HasEducationPlan":false,
"MembersUrl":"https://student.ulife.com.br/DisciplineMembersList/Index?cd=TZuLq3PpEtt31xeZ06SGsw%3d%3d",
"CalendarUrl":"https://student.ulife.com.br/Calendar?cd=TZuLq3PpEtt31xeZ06SGsw%3d%3d",
"EvaluationUrl":"https://www.ulife.com.br/Student/PAT/Grades.aspx?cd=TZuLq3PpEtt31xeZ06SGsw%3d%3d",
"DiscussionUrl":"https://www.ulife.com.br/Student/PAT/DiscussionForum.aspx?cld=TZuLq3PpEtt31xeZ06SGsw%3d%3d",
"ContentUrl":"https://student.ulife.com.br/Lesson/Index?cd=TZuLq3PpEtt31xeZ06SGsw%3d%3d",
"GradeUrl":"https://student.ulife.com.br/Grade/Index?cd=TZuLq3PpEtt31xeZ06SGsw%3d%3d&st=ACFdBAwNYJcCXLzBpH3P2A%3d%3d",
"CourseUrl":"https://www.ulife.com.br/OrganizationCourse/OrganizationCourseDetail.aspx?o=NaltKyU6OKV8RcmQsxNuow%3d%3d&c=P56mWzUmVubJFqSYQZZBTA%3d%3d",
"ClassUrl":"/classes/classpage.aspx?ClassId=4hQOURLROzT45urCqEmG0g%3d%3d",
"PerformanceUrl":null,
"FrequencyUrl":"/Frequency/ClassDisciplineFrequency.aspx?d=g%2fod%2bQ9Lh69WQPUqIkHAVA%3d%3d&cn=fH8YHJoBctFFuBbe3vjGmNudp6v634AToqCmBdiwWpY%3d",
"DisciplineUrl":"/PAT/Discipline.aspx?p=GblZUfavs44hSRDDXEjIoGIn%2b0Yf4zcvALpxLCpX7CQ%3d",
"ChallengeUrl":"https://www.ulife.com.br/Controls/EnadeChallenge.aspx?scd=22397399",
"ViewMembers":true,
"NewFeatureEnabled":true,
"StudentClassDisciplineID":22397399,
"StudentID":0,
"ClassDisciplineID":711475,
"StudentClassDisciplineStatus":1,
"DisciplineContentID":609158,
"HasDisciplineContent":true,
"CurriculumEducationPlanID":0,
"OrganizationID":2418,
"DisciplineID":345325,
"DisciplineName":"LAI: Identidade, Criatividade e Resolução de Problemas",
"IsMultiOrganization":false,
"Enrolled":false,
"ClassID":586927,
"ClassName":"CCP1AN-CMA1-740671",
"GradeID":3432,
"GradeName":"Graduação (Prática)",
"OrganizationCourseID":36597,
"OrganizationCourseName":"Ciência da Computação",
"OriginOrganizationCourseID":0,
"ModuleStepID":246695,
"ModuleStepName":"UNIBH_2019/1",
"ModuleStepStartDate":"2019-01-01T00:00:00",
"ModuleStepEndDate":"2019-07-31T00:00:00",
"HasFrequency":false,
"HasEvaluationPeriod":false,
"HasEnade":false,
"StudentActive":true,
"EnadeChallengeTypeId":0,
"HasDiscussionForum":false,
"NewLessonCategoryCount":3,
"Year":0,
"DisciplineContentCoverImageUrl":"",
"LessonCount":2
}
],
"Success":true,
"ErrorMessage":null,
"ErrorType":0
}
使用此类
public class Result {
private String DisciplineName;
public String getDisciplineName() {
return DisciplineName;
}
public void setDisciplineName(String disciplineName) {
DisciplineName = disciplineName;
}
public Result() {
}
public Result(String DisciplineName) {
this.DisciplineName = DisciplineName;
}
@Override
public String toString() {
return "Result{" +
"disciplineName='" + DisciplineName + '\'' +
'}';
}
}
这是Main:
public class Main {
public static void main(String[] args) {
Ulife u = new Ulife();
try {
ObjectMapper mapper = new ObjectMapper();
// TypeFactory typeFactory = mapper.getTypeFactory();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
JsonNode node = mapper.readTree(u.getDisciplinas());
//node = node.get("Result");
//System.out.println(node.get("Result").get(0).get("DisciplineName"));
List<Result> myObjects = Arrays.asList(mapper.readValue(node.toString(), Result[].class));
//List<Result> myObjects = Arrays.asList(mapper.readValue(node.get("Result").traverse(), Result[].class));;
//List<Result> someClassList = mapper.readValue(node.traverse(), typeFactory.constructCollectionType(List.class, Result.class));
//List<Result> list = mapper.treeToValue(node, typeRef);
System.out.println(myObjects);
// System.out.println(node.get("DisciplineName").asText());
}
catch (Exception e){
System.out.println(e.getMessage());
}
}
}
但是,当我打印出对象时,它们给了我所有的空值...我错过了什么吗?我不需要Result数组之外的值,但是我从DisciplineName开始,以了解杰克逊的工作原理,我的计划是在工作时添加其他一些节点。
我尝试了一些我在互联网上看到的示例,包括在stackoverflow上的示例,但我没有得到任何帮助。
谢谢。
答案 0 :(得分:0)
考虑使用Jackson批注。 另外,学习并遵守JavaBean命名约定。
这是一些示例代码:
@JsonIgnoreProperties(ignoreUnknown = true)
public class Result {
@JsonProperty
private String disciplineName;
public String getDisciplineName() {
return disciplineName;
}
public void setDisciplineName(String newValue) {
disciplineName = newValue;
}
public Result() {
}
public Result(String disciplineNameValue) {
disciplineName = disciplineNameValue;
}
@Override
public String toString() {
return "Result{" +
"disciplineName='" + disciplineName + '\'' +
'}';
}
}
某些方法的代码:
Result result;
ObjectMapper mapper = new ObjectMapper();
result = objectMapper.readValue(u.getDisciplinas(), Result.class);
这假设u.getDisciplinas()
返回一个字符串