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.
相关推荐
Dillon Dong8 小时前
Django + uWSGI 部署至 Ubuntu 完整指南
python·ubuntu·django
丸码9 小时前
Java异常体系全解析
java·开发语言
q***72199 小时前
PHP使用Redis实战实录2:Redis扩展方法和PHP连接Redis的多种方案
开发语言·redis·php
k***82519 小时前
python爬虫——爬取全年天气数据并做可视化分析
开发语言·爬虫·python
IMPYLH9 小时前
Lua 的 require 函数
java·开发语言·笔记·后端·junit·lua
曾经的三心草9 小时前
基于正倒排索引的Java文档搜索引擎1-实现索引模块-实现Parser类
java·开发语言·搜索引擎
new_dev9 小时前
Python网络爬虫从入门到实战
爬虫·python·媒体
q***01659 小时前
Python爬虫完整代码拿走不谢
开发语言·爬虫·python
顺心而行...9 小时前
一些问题记录
开发语言
今天没有盐9 小时前
Python算法实战:从滑动窗口到数学可视化
python·pycharm·编程语言