如何排除对象的JSON属性(在输出类中)?

时间:2017-07-28 11:51:22

标签: java json object properties jackson

假设我有一段代码用于JSON将使用的输出类:

StudentSummary.java

public class StudentSummary {

StudentList studentList;

//getters setters
}

然后是StudentList

StudentList.java
public class StudentList {

int numberOfStudents;
int totalExpenditures;
List<Student> students;

//getters setters
}

进行服务调用后,我得到了这个JSON输出:

{ 
 "studentSummary": {
     "studentList": {
     "numberOfStudents": 500
     "totalExpenditures": 250000
     "students": [ {
        //students listed
    }
  ]
 }
}

我想要做的是在StudentSummary输出类中从JSON中排除学生列表,所以它只是这样:

{ 
 "studentSummary": {
     "studentList": {
     "numberOfStudents": 500
     "totalExpenditures": 250000
         }
}

我尝试使用(在StudentSummary输出类中)@JsonIgnore@JsonProperties,指定只排除"studentList.students",但这并没有做任何事情。

编辑:进一步澄清,为什么我无法在StudentList类中进行更改,因为它被其他服务使用,而StudentSummary类的服务是不同的服务,所以我只能在后一类中进行更改,而不修改以前的服务。

6 个答案:

答案 0 :(得分:1)

如果您无法更改类的源代码,则需要创建Mixin类以省略Studentlist ...

按照以下链接获取Jackson Mixin ..

<强> How can I tell jackson to ignore a property for which I don't have control over the source code?

答案 1 :(得分:1)

摘要

这将解决您的问题:

public class StudentSummary {

    @JsonIgnoreProperties(value = { "students" })
    public StudentList studentList = new StudentList();

您无需编辑StudentList课程。

所有测试类定义

<强>学生

public class Student {

    String name = "Student" + Math.random()*100;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

<强> StudentList

public class StudentList {

    int numberOfStudents;
    int totalExpenditures;
    List<Student> students = new ArrayList<>();

    public StudentList(){
        for(int i =0; i<10; i++){
            students.add(new Student());
        }
    }

    public int getNumberOfStudents() {
        return numberOfStudents;
    }

    public void setNumberOfStudents(int numberOfStudents) {
        this.numberOfStudents = numberOfStudents;
    }

    public int getTotalExpenditures() {
        return totalExpenditures;
    }

    public void setTotalExpenditures(int totalExpenditures) {
        this.totalExpenditures = totalExpenditures;
    }

    public List<Student> getStudents() {
        return students;
    }

    public void setStudents(List<Student> students) {
        this.students = students;
    }

    //getters setters
}

<强> StudentSummary

public class StudentSummary {

    @JsonIgnoreProperties(value = { "students" })
    public StudentList studentList = new StudentList();

    public StudentSummary(){
        studentList = new StudentList();
    }

    public StudentList getStudentList() {
        return studentList;
    }

    public void setStudentList(StudentList studentList) {
        this.studentList = studentList;
    }

    //getters setters
}

主要类

ObjectMapper mapper = new ObjectMapper();

StudentSummary summary = new StudentSummary();
String test = mapper.writeValueAsString(summary);
System.out.println(test);
System.out.print("DONE");

答案 2 :(得分:0)

在JSON序列化时,您可以使用@JsonIgnoreProperties或@JsonIgnore忽略类的任何属性。例如,

***public class StudentList {
int numberOfStudents;
int totalExpenditures;
@JsonIgnore
List<Student> students;
//getters setters
}***

***@JsonIgnoreProperties(value = { "students" })
public class StudentList {
int numberOfStudents;
int totalExpenditures;
List<Student> students;
//getters setters
}***

答案 3 :(得分:0)

我相信你不能触及StudentList课程的问题,所有内容都应该从StudentSummary课程处理。我的想法是创建另一个类SpecialStudent并覆盖学生列表的setter / getter,然后将@JsonIgnore属性放在它们上面。以下是相同的例子。如果有帮助,请告诉我

class SpecialStudent extends StudentList{
    @JsonIgnore
    @Override
    public List<Student> getStudents()
    {
        return students;
    }
    /**
     * @param students the students to set
     */
    @Override
    @JsonIgnore
    public void setStudents(List<Student> students)
    {
        this.students = students;
    }
}

学生摘要将如下所示

public class StudentSummary
{
    SpecialStudent studentList;

    /**
     * @return the studentList
     */
    public StudentList getStudentList()
    {
        return studentList;
    }

    /**
     * @param studentList the studentList to set
     */
    public void setStudentList(SpecialStudent studentList)
    {
        this.studentList = studentList;
    }

}

测试类

StudentSummary sum = new  StudentSummary();
        SpecialStudent l = new SpecialStudent();
        l.setNumberOfStudents(4);
        l.setTotalExpenditures(12);
        sum.setStudentList(l);


        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(sum);

        System.out.println(json);

<强>输出

{
  "studentList" : {
    "numberOfStudents" : 4,
    "totalExpenditures" : 12
  }
}

答案 4 :(得分:0)

我建议阅读here

基本上,如果你不能修改类本身的任何东西,那么你需要告诉jackson全局如何在序列化配置上处理这个(通过对象映射器上的混合注释)

答案 5 :(得分:-1)

因为你无法改变StudentList;您不需要在StudentSummary中包含StudentList实例。所以,您可以进行如下所示的某种映射:

 public class StudentSummary {

    int numberOfStudents;
    int totalExpenditures;

    public StudentSummary (){
         StudentList studentList = new StudentList();
         this.numberOfStudents = studentList.getNumberOfStudents();
         this.totalExpenditures = studentList.getTotalExpenditures();
    }

    //getters setters for numberOfStudents & totalExpenditures
  }