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.
相关推荐
吾爱星辰1 小时前
Kotlin 处理字符串和正则表达式(二十一)
java·开发语言·jvm·正则表达式·kotlin
ChinaDragonDreamer1 小时前
Kotlin:2.0.20 的新特性
android·开发语言·kotlin
FreakStudio1 小时前
全网最适合入门的面向对象编程教程:56 Python字符串与序列化-正则表达式和re模块应用
python·单片机·嵌入式·面向对象·电子diy
IT良1 小时前
c#增删改查 (数据操作的基础)
开发语言·c#
丶21361 小时前
【CUDA】【PyTorch】安装 PyTorch 与 CUDA 11.7 的详细步骤
人工智能·pytorch·python
Kalika0-02 小时前
猴子吃桃-C语言
c语言·开发语言·数据结构·算法
_.Switch2 小时前
Python Web 应用中的 API 网关集成与优化
开发语言·前端·后端·python·架构·log4j
代码雕刻家2 小时前
课设实验-数据结构-单链表-文教文化用品品牌
c语言·开发语言·数据结构
一个闪现必杀技2 小时前
Python入门--函数
开发语言·python·青少年编程·pycharm
Fan_web2 小时前
jQuery——事件委托
开发语言·前端·javascript·css·jquery