当前位置:网站首页>Jackson - how to convert the array string with only one map object to list < map >
Jackson - how to convert the array string with only one map object to list < map >
2022-06-12 05:53:00 【Amos-Chen】
background
There is a string that looks like this :
[{
"success": {
"description": "Welcome to JSON Viewer",
"code": 200
},
"message": "this is a message"
}]
Want to convert it to List<Map> object , So I wrote :
// json For the string mentioned above
List<Map<String, Object>> mapList = new ObjectMapper().convertValue(json, new TypeReference<List<Map<String, Object>>>() {
});
Error report in execution :Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of ‘java.util.ArrayList<java.util.Map<java.lang.String,java.lang.Object>>’ out of VALUE_STRING token
I checked the reason because : When you switch There is only one object in the array , This is forbidden by default . Need modification jackson Configuration of .
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
List<Map<String, Object>> mapList = mapper.convertValue(json, new TypeReference<List<Map<String, Object>>>() {
});
The execution still reports an error :
Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `java.util.LinkedHashMap` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('[{
"success": {
"description": "Welcome to JSON Viewer",
"code": 200
},
"message": "this is a message"
}]')
A simple translation : Unable to construct LinkedHashMap Example ( Although there is at least one Creator ).
Re reform : No longer use convertValue Method , change to the use of sth. readValue Method , Direct fix .
solve
// json For the string mentioned above
// However, there will be detected exceptions
List<Map<String, Object>> mapList = new ObjectMapper().readValue(json, new TypeReference<List<Map<String, Object>>>() {
});
// Print converted results
System.out.println(mapList);
Print the results :
[{success={description=Welcome to JSON Viewer, code=200}, message=this is a message}]
边栏推荐
- yolov5
- Analysis of pointer
- Understanding of distributed transactions
- WiFi band resources
- IO to IO multiplexing from traditional network
- Leetcode simple problem: converting an integer to the sum of two zero free integers
- C WMI query remote Win32_ Operatingsystem class
- nRF52832自定义服务与特性
- 虚函数与纯虚函数的关系
- Database experiment I: data definition experiment guide
猜你喜欢

GRP development: four communication modes of GRP

Introduction to redis high availability

Webrtc AEC process analysis

Leetcode simple problem: converting an integer to the sum of two zero free integers

MySQL 主从,6 分钟带你掌握

登录验证过滤器

Redis memory obsolescence strategy

Halcon 用点来拟合平面

The application could not be installed: INSTALL_ FAILED_ TEST_ ONLY

The application could not be installed: INSTALL_FAILED_TEST_ONLY
随机推荐
Flutter monitors application lifecycle status
[road of system analyst] collection of wrong topics in software engineering chapters
Who is more fierce in network acceleration? New king reappeared in CDN field
[daily question on niuke.com] two point search
The application could not be installed: INSTALL_FAILED_TEST_ONLY
Execute sh script to prompt "[[: not found" solution. The difference between Bash and sh
Niuke daily question -day1
[JS knowledge] easily understand JS anti shake and throttling
WiFi protocol and ieee905 protocol learning details
Go interface implementation principle [advanced level]
tkinter使用WebView2网页组件(续篇)
What is the lszrz protocol used at ordinary times? Talk about xmodem/ymodem/zmodem
WiFi band resources
Greenplum【问题 05】Greenplum Streaming Server自定义客户端问题处理(不断增加ing)
China Aquatic Fitness equipment market trend report, technical innovation and market forecast
软件项目架构简单总结
Simple introduction to key Wizard
nRF52832自定义服务与特性
Go 接口实现原理【高阶篇】
Oracle EBS interface/api (34) - update vendor API