猿问

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

我遇到了一个问题,我试图正确地生成我的 json 格式。所以,我得到了自定义模式的第一部分,但我需要一些帮助来修复第二部分,即小时、每周、每年的数组列表,以将值列为数组,我一直在解决这个问题。谁能帮我解决这个问题。谢谢!



素胚勾勒不出你
浏览 64回答 1
1回答

扬帆大鱼

您需要从步骤创建列表并使用writeObjectField方法序列化此列表。您可以在下面找到示例实现:class JobSerializer extends StdSerializer<Salary> {&nbsp; &nbsp; public JobSerializer() {&nbsp; &nbsp; &nbsp; &nbsp; this(Salary.class);&nbsp; &nbsp; }&nbsp; &nbsp; public JobSerializer(Class<Salary> t) {&nbsp; &nbsp; &nbsp; &nbsp; super(t);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void serialize(Salary value, JsonGenerator jgen, SerializerProvider provider) throws IOException {&nbsp; &nbsp; &nbsp; &nbsp; jgen.writeStartObject();&nbsp; &nbsp; &nbsp; &nbsp; jgen.writeStringField("id", value.jobClassCd + value.payGrade + value.jobGroup);&nbsp; &nbsp; &nbsp; &nbsp; jgen.writeStringField("label", value.jobClassTitle + "( " + value.jobClassCd + value.payGrade + ") " + value.jobGroup);&nbsp; &nbsp; &nbsp; &nbsp; jgen.writeStringField("pay_grade_description_link", "payGrade");&nbsp; &nbsp; &nbsp; &nbsp; jgen.writeStringField("job_type", value.sectionToDisplay);&nbsp; &nbsp; &nbsp; &nbsp; jgen.writeStringField("mou", value.mou);&nbsp; &nbsp; &nbsp; &nbsp; jgen.writeStringField("mou_description", value.mouDescription);&nbsp; &nbsp; &nbsp; &nbsp; jgen.writeStringField("special_notes", value.specialNotes);&nbsp; &nbsp; &nbsp; &nbsp; jgen.writeStringField("salary_range_min_step_message", value.salaryRangeMinStepMessage);&nbsp; &nbsp; &nbsp; &nbsp; List<String> jobs = Arrays.asList(value.step1, value.step2, value.step3, value.step4, value.step5, value.step6, value.step7,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value.step8, value.step9, value.step10, value.step11, value.step12, value.step13, value.step14,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value.step15, value.step16, value.step17, value.step18, value.step19, value.step20);&nbsp; &nbsp; &nbsp; &nbsp; jgen.writeObjectField(value.rateType, jobs);&nbsp; &nbsp; &nbsp; &nbsp; jgen.writeEndObject();&nbsp; &nbsp; }}此外,您可以@JsonSerialize从类中的所有属性中删除Salary。它不是必需的,可能会造成混淆。
随时随地看视频慕课网APP

相关分类

Java
我要回答