java jackson deseriarize map field with object key and list value

To deserialize a JSON object where the keys are objects and the values are lists into a Java Map using Jackson, you can follow a similar approach as before, but with some modifications to handle lists as values.

Let's assume you have the following JSON:

复制代码

jsonCopy code

{ "{\"key1\":\"value1\"}": ["foo", "bar"], "{\"key2\":\"value2\"}": ["baz", "qux"] }

Here, the keys are JSON objects, and the values are lists of strings. To deserialize this JSON into a Java Map, you can create custom deserializers for both the key objects and the list values.

First, define a class to represent the key objects:

复制代码

javaCopy code

import com.fasterxml.jackson.databind.annotation.JsonDeserialize; @JsonDeserialize(using = KeyValueDeserializer.class) public class KeyValue { private final String key; public KeyValue(String key) { this.key = key; } public String getKey() { return key; } }

Now, create the custom deserializer KeyValueDeserializer:

复制代码

javaCopy code

import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.JsonDeserializer; import java.io.IOException; public class KeyValueDeserializer extends JsonDeserializer<KeyValue> { @Override public KeyValue deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { String key = p.readValueAsTree().toString(); return new KeyValue(key); } }

Next, create a class to represent the list values:

复制代码

javaCopy code

import java.util.List; public class ListValue { private List<String> values; public List<String> getValues() { return values; } public void setValues(List<String> values) { this.values = values; } }

Now, you can use Jackson's ObjectMapper to deserialize the JSON into a Java Map:

复制代码

javaCopy code

import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import java.io.IOException; import java.util.Map; public class JsonToObjectMapExample { public static void main(String[] args) throws IOException { String json = "{ \"{\\\"key1\\\":\\\"value1\\\"}\": [\"foo\", \"bar\"], \"{\\\"key2\\\":\\\"value2\\\"}\": [\"baz\", \"qux\"] }"; ObjectMapper objectMapper = new ObjectMapper(); Map<KeyValue, ListValue> map = objectMapper.readValue(json, new TypeReference<Map<KeyValue, ListValue>>() {}); System.out.println("Deserialized Map: " + map); } }

In this example:

  • KeyValue represents the keys in the JSON object and is annotated with @JsonDeserialize(using = KeyValueDeserializer.class) to specify the custom deserializer.
  • KeyValueDeserializer is a custom deserializer that extracts the key from the JSON object.
  • ListValue represents the list values in the JSON object.
  • JsonToObjectMapExample demonstrates how to deserialize the JSON into a Map using Jackson's ObjectMapper, with values of type ListValue.
相关推荐
外收内放20 分钟前
Python基础语法练习题(69)
开发语言·python
lojox25 分钟前
深入理解 C++ STL:vector 底层实现、迭代器失效与 list 特性
开发语言·c++
TomEval25 分钟前
【测AI】第11篇:深度学习基础 —— 神经网络是怎么工作的
人工智能·python·深度学习·神经网络·机器学习·aigc
iotxiaohu27 分钟前
用 cflow 查看 C 函数调用关系
c语言·开发语言·cflow
zhang-ge27 分钟前
python包和项目管理器uv在ubuntu下的安装与使用
开发语言·python·uv
for_ever_love__38 分钟前
LangChain 降级、重试与超时:让服务在模型抽风时还能用
python·自然语言处理·langchain·agent·超时·重试·with_retry
MoRanzhi12031 小时前
第 8 篇 · 机器学习建模与实验评估:建模流程与对照实验
python·机器学习·scikit-learn·模型评估·交叉验证·shap·消融实验
棉猴1 小时前
玩游戏学Python9-算术运算符应用1
python·游戏·pygame·玩游戏·游戏编程
微笑星河q1 小时前
C++类和对象(下)初始化列表、类型转换、static、友元、内部类、匿名对象
开发语言·c++
凤年徐1 小时前
【Python】Python 语法速通指南
开发语言·python