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.
相关推荐
leaves falling5 小时前
C语言内存函数-
c语言·开发语言
程序员:钧念5 小时前
深度学习与强化学习的区别
人工智能·python·深度学习·算法·transformer·rag
数据与后端架构提升之路5 小时前
TeleTron 源码揭秘:如何用适配器模式“无缝魔改” Megatron-Core?
人工智能·python·适配器模式
至为芯6 小时前
IP6537至为芯支持双C口快充输出的45W降压SOC芯片
c语言·开发语言
hele_two6 小时前
快速幂算法
c++·python·算法
小羊羊Python7 小时前
SoundMaze v1.0.1正式发布!
开发语言·c++
浩瀚地学7 小时前
【Java】JDK8的一些新特性
java·开发语言·经验分享·笔记·学习
l1t7 小时前
利用DeepSeek将python DLX求解数独程序格式化并改成3.x版本
开发语言·python·算法·数独
yugi9878388 小时前
基于遗传算法优化主动悬架模糊控制的Matlab实现
开发语言·matlab
moxiaoran57539 小时前
Go语言的错误处理
开发语言·后端·golang