简体   繁体   English

如何在java / jackson中的自定义模式之前获取json结果?

[英]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. 我遇到了一个问题,我正试图正确地生成我的json格式。 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. 您需要从步骤创建列表并使用writeObjectField方法序列化此列表。 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. 此外,您可以从Salary类中的所有属性中删除@JsonSerialize It is not required and can be confusing. 它不是必需的,可能会令人困惑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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