Jackson解析同时包含匿名和具名字段的数组

huangapple go评论61阅读模式
英文:

Jackson parse array with both anonymous and named field

问题

@ JsonFormat(shape = JsonFormat.Shape.ARRAY)
public class Event {

@JsonProperty
String event;

@JsonProperty("event-id")
String eventId;

@JsonProperty
Map<String, Object> properties;

@Override
public String toString() {
    ObjectMapper mapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
    try {
        String json = mapper.writeValueAsString(this);
        return json;
    } catch (JsonProcessingException e) {
        throw new RuntimeException(e);
    }
}

}

英文:

I would like to parse the following json string using Jackson ObjectMapper:

[&quot;EVENT&quot;,&quot;event-id&quot;,{&quot;content&quot;:&quot;{}&quot;,&quot;created_at&quot;:1677781808,&quot;tags&quot;:[[&quot;t&quot;,&quot;tag1&quot;]]}]

I was trying the following model class:

@JsonFormat(shape= JsonFormat.Shape.ARRAY)
public class Event {

    @JsonProperty
    String event;

    @JsonProperty
    String eventId;



    @Override
    public String toString() {
        ObjectMapper mapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
        try {
            String json = mapper.writeValueAsString(this);
            return json;
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}

Trying to parse like this:

 Event event = mapper.readValue(message.getPayload().getBytes(StandardCharsets.UTF_8), Event.class);

I get the following error:

Unexpected token (START_OBJECT), expected END_ARRAY: Unexpected JSON values; expected at most 2 properties (in JSON Array)
 at [Source: (byte[])&quot;[&quot;EVENT&quot;,&quot;3c1dbf5f-b2b0-478d-9b18-2721a90d5f29&quot;,{&quot;content&quot;:...

答案1

得分: 1

你可以在你的 Event 类上添加 JsonIgnoreProperties 注解,以忽略在反序列化期间不包含在你的类中的 JSON 属性的处理:

@Data
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonFormat(shape = JsonFormat.Shape.ARRAY)
public class Event {

    String event;
    String eventId;
}

Event event = mapper.readValue(message.getPayload()
                                      .getBytes(StandardCharsets.UTF_8), Event.class);
// 输出结果为 [ "EVENT", "event-id" ]
System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(event));

在上述代码中,Event 类使用 @JsonIgnoreProperties 注解,设置 ignoreUnknowntrue,以便忽略不包含在类中的 JSON 属性。

英文:

You can add the JsonIgnoreProperties annotation to your Event class so ignoring the processing of JSON properties not included in your class read during deserialization:

@Data
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonFormat(shape= JsonFormat.Shape.ARRAY)
public class Event {

    String event;
    String eventId;
}

Event event = mapper.readValue(message.getPayload()
                                      .getBytes(StandardCharsets.UTF_8), Event.class);
//it prints [ &quot;EVENT&quot;, &quot;event-id&quot; ]     
System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(event));

huangapple
  • 本文由 发表于 2023年3月4日 04:16:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75631525.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定