在Jackson中映射动态json对象字段? [英] Mapping a dynamic json object field in Jackson?

查看:508
本文介绍了在Jackson中映射动态json对象字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在以下架构中有json对象:

I have json objects in the following schema:

{
  name: "foo",
  timestamp: 1475840608763,
  payload:
  {
     foo: "bar"
  }
}

这里,有效载荷字段包含一个嵌入的json对象,该对象的模式是动态的,每个都是不同的时间。

Here, the payload field contains an embedded json object, and the schema of this object is dynamic, and different each time.

有效负载对象是从不同API服务获得的原始输出,以及不同API服务的不同方法。无法将其映射到所有可能的值。

The payload object is the raw output obtained from different API services, and different methods of different API services. It isn't possible to map it to all possible values.

是否可以使用如下的java类:

Is it possible to have a java class such as the following:

public class Event
{
    public String name;
    public long timestamp;
    public JsonObject payload;
}

或者那些东西,所以我可以收到基本架构并处理它,然后将它发送到相关的类,它将有效载荷转换为适当的预期类?

Or something along those lines, so I can receive the basic schema and process it, then send it to the relevant class which will convert payload to its appropriate expected class?

推荐答案

使用 JsonNode



您可以使用 JsonNode 来自 com .fasterxml.jackson.databind package:

Using JsonNode

You could use JsonNode from the com.fasterxml.jackson.databind package:

public class Event {

    public String name;
    public long timestamp;
    public JsonNode payload;

    // Getters and setters
}

然后解析它使用:

String json = "{\"name\":\"foo\",\"timestamp\":1475840608763,"
            + "\"payload\":{\"foo\":\"bar\"}}";

ObjectMapper mapper = new ObjectMapper();
Event event = mapper.readValue(json, Event.class);



JsonNode 映射到POJO



例如,考虑要映射 JsonNode 实例到以下类:

Mapping JsonNode to a POJO

Consider, for example, you want to map the JsonNode instance to the following class:

public class Payload {

    private String foo;

    // Getters and setters
}

它可以是使用以下代码实现:

It can be achieved with the following piece of code:

Payload payload = mapper.treeToValue(event.getPayload(), Payload.class);



考虑 Map< String,Object>



根据您的要求,您可以使用 Map< String,Object> 代替 JsonNode

public class Event {

    public String name;
    public long timestamp;
    public Map<String, Object> payload;

    // Getters and setters
}

如果您需要要将 Map< String,Object> 转换为POJO,请使用:

If you need to convert a Map<String, Object> to a POJO, use:

Payload payload = mapper.convertValue(event.getPayload(), Payload.class);

根据Jackson 文档 convertValue()方法在功能上类似于首先将给定值序列化为JSON,然后将JSON数据绑定到给定类型的值,但应该更高效,因为完全序列化不需要(需要)发生。但是,相同的转换器(序列化器和反序列化器)将用于数据绑定,这意味着相同的对象映射器配置可用。

According to the Jackson documentation, the convertValue() method is functionally similar to first serializing given value into JSON, and then binding JSON data into value of given type, but should be more efficient since full serialization does not (need to) occur. However, same converters (serializers and deserializers) will be used as for data binding, meaning same object mapper configuration works.

这篇关于在Jackson中映射动态json对象字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆