简体   繁体   中英

How to get json results prior to my custom schema in java/jackson?

I a coming to an problem where I am trying to result my json format properly. So, I got the first part of my custom schema, but I need some help to fix the second part which is the array list of hours,weekly, annual to list values as array and I am stuck to solve this issue. can anyone help me solve this. thanks!

You need to create list from steps and use writeObjectField method to serialise this list. Below you can find example implementation:

class JobSerializer extends StdSerializer<Salary> {

    public JobSerializer() {
        this(Salary.class);
    }

    public JobSerializer(Class<Salary> t) {
        super(t);
    }

    @Override
    public void serialize(Salary value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
        jgen.writeStartObject();
        jgen.writeStringField("id", value.jobClassCd + value.payGrade + value.jobGroup);
        jgen.writeStringField("label", value.jobClassTitle + "( " + value.jobClassCd + value.payGrade + ") " + value.jobGroup);

        jgen.writeStringField("pay_grade_description_link", "payGrade");
        jgen.writeStringField("job_type", value.sectionToDisplay);
        jgen.writeStringField("mou", value.mou);
        jgen.writeStringField("mou_description", value.mouDescription);
        jgen.writeStringField("special_notes", value.specialNotes);
        jgen.writeStringField("salary_range_min_step_message", value.salaryRangeMinStepMessage);

        List<String> jobs = Arrays.asList(value.step1, value.step2, value.step3, value.step4, value.step5, value.step6, value.step7,
                value.step8, value.step9, value.step10, value.step11, value.step12, value.step13, value.step14,
                value.step15, value.step16, value.step17, value.step18, value.step19, value.step20);
        jgen.writeObjectField(value.rateType, jobs);

        jgen.writeEndObject();

    }
}

Also, you can remove @JsonSerialize from all properties in Salary class. It is not required and can be confusing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM