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.
相关推荐
yuyu_03041 分钟前
AI识别手部异常
python·ai
码界奇点1 分钟前
基于Java GUI和Access数据库的图书馆管理系统设计与实现
java·开发语言·数据库·毕业设计·源代码管理
袁袁袁袁满4 分钟前
基于亮数据MCP与LangGraph集成实现爬虫自动化
爬虫·python·网络爬虫·数据采集·爬虫实战·自动化采集·爬虫案例
Moshow郑锴6 分钟前
JAVA JDK26新特性分析 - 一个注重性能优化、生产就绪和前瞻性安全的版本
java·开发语言·jvm
非凡ghost7 分钟前
proDAD ReSpeedr:专业视频变速编辑的利器
java·网络·windows·python·音视频·软件需求
Oueii11 分钟前
持续集成/持续部署(CI/CD) for Python
jvm·数据库·python
Jackey_Song_Odd13 分钟前
Part 1:Python语言核心 - 缩进与代码块
开发语言·python
码农时代者21 分钟前
拒绝重复造轮子!开发者如何靠“高质量源码”实现项目高效交付?
java·python·php
郝学胜-神的一滴28 分钟前
深度学习入门基石:PyTorch张量核心技术全解析
人工智能·pytorch·python·深度学习·算法·机器学习
@PHARAOH29 分钟前
HOW - Go 开发入门(二)
开发语言·后端·golang