简体   繁体   English

为 Z1D78DC8ED51214E5018B5114AE24 创建自定义 java json spring 引导解析器<string, integer></string,>

[英]Create custom java json spring boot parser for map<string, integer>

Error JSON parse error: Cannot coerce empty String ("") to element of `java.util.LinkedHashMap<java.lang.String,java.lang.Integer>`错误JSON parse error: Cannot coerce empty String ("") to element of `java.util.LinkedHashMap<java.lang.String,java.lang.Integer>`

when the http request is like field: {"one": 1} then it works当 http 请求类似于field: {"one": 1}那么它可以工作

however, the field is nullable and sometimes there comes an http request where the field is like field: ""但是,该field可以为空,有时会出现一个 http 请求,其中字段类似于field: ""

is it possible to create an converter which checks if the string is null or blank and then returns null without trying to parse it to the map and get an error?是否可以创建一个转换器来检查字符串是 null 还是空白,然后返回 null 而不尝试将其解析为 map 并得到错误?

You can use Optional I guess.我猜你可以使用可选的。

And yes, you can convert it.是的,你可以转换它。

Here is an example of trying to set a value that might be Null without the use of Optional.这是一个尝试在不使用 Optional 的情况下设置可能为 Null 的值的示例。

Am trying to set spent_hours for example after getting the value from node which is Map<String, Object>例如,在从Map<String, Object> node获取值之后,我试图设置spent_hours时间

setSpent_hours(node.get("total_spent_hours")== null ? "N/A" :node.get("total_spent_hours").toString());

This is basically setting the value to a string of N/A instant of keeping the value null.这基本上是将值设置为保持值 null 的N/A瞬间字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM