英文:
Unrecognized field exception due to Incorrectly formatted O/P of objectMapper.writeValueAsString
问题
以下是翻译好的内容:
我正在尝试使用Jackson的ObjectMapper将我的哈希映射(JSON)反序列化为POJO类。以下是哈希映射:
List<Object> setJSONValues = new ArrayList<Object>(Arrays.asList(requestObj));
List<String> setJSONKeys = apiUtility.readJSONKeys(new File("ABC.csv"));
HashMap<String, Object> requestMap = new HashMap<String, Object>();
if (setJSONKeys.size() == setJSONValues.size()) {
for (int i = 0; i < setJSONKeys.size(); i++) {
requestMap.put(setJSONKeys.get(i), setJSONValues.get(i));
}
}
我想使用这个requestMap
对象映射到我的POJO类中,使用以下代码:
objectMapper.readValue(objectMapper.writeValueAsString(requestMap), MyRequestDTO.class);
我得到以下错误:
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field
" "apptDateTime"" (class Collector.MyRequestDTO)
上述错误是因为我objectMapper.writeValueAsString(requestMap)
的输出是:
{" "apptDateTime"":""2019-03-19 10:00:00"",""meter"":""8682""
添加哈希映射的输出:
for (String s:requestMap.keySet())
System.out.println("Key is "+s+" Value is "+requestMap.get(s));
输出:Key is "apptDateTime" Value is "2019-03-19 10:00:00" Key is
"meter" Value is "8682"
英文:
I am trying to deserialize my hashmap(JSON) to POJO class using Jackson - ObjectMapper . Below is the hashmap :
List<Object> setJSONValues = new ArrayList<Object>(Arrays.asList(requestObj));
List<String> setJSONKeys = apiUtility.readJSONKeys(new File("ABC.csv"));
HashMap<String, Object> requestMap = new HashMap<String, Object>();
if (setJSONKeys.size() == setJSONValues.size()) {
for (int i = 0; i < setJSONKeys.size(); i++) {
requestMap.put(setJSONKeys.get(i), setJSONValues.get(i));
}
}
I want to use this requestMap into my POJO class using object mapper see below :
objectMapper.readValue(objectMapper.writeValueAsString(requestMap), MyRequestDTO.class);
I get below error :
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field
" "apptDateTime"" (class Collector.MyRequestDTO)
Above error is coming because O/P of my objectMapper.writeValueAsString(requestMap)
is :
{" "apptDateTime"":""2019-03-19 10:00:00"",""meter"":""8682""
Adding Hashmap O/P :
for (String s:requestMap.keySet())
System.out.println("Key is "+s+"Value is "+requestMap.get(s));
Output : Key is "apptDateTime"Value is "2019-03-19 10:00:00" Key is
"meter"Value is "8682"
答案1
得分: 1
你用于读取 keys
的实用方法不如您期望的工作(就是这个方法:)
List<String> setJSONKeys = apiUtility.readJSONKeys(new File("ABC.csv"));
它返回的键和值被双引号包裹,所以本应是 "apptDateTime"
的键实际上被返回为 " \"apptDateTime\""
。您可以在您添加的调试输出中看到这一点:您并未在键或值周围添加引号,但输出仍然显示了引号。
您可以通过以下方式绕过此错误,但最好修复首次返回意外数据的函数。
String key = removeQuotes(setJSONKeys.get(i));
String value = removeQuotes(setJSONValues.get(i));
requestMap.put(key, setJSONValues.get(i));
...
String removeQuotes(String key) {
key = key.trim();
key = key.substring(1, key.length() - 1); // 移除引号
return key.trim();
}
请注意,这里只是对您给出的代码和描述进行的翻译。
英文:
Your utility method for reading the keys
does not work as you expect (this one:)
List<String> setJSONKeys = apiUtility.readJSONKeys(new File("ABC.csv"));
It is returning keys and values wrapped in double quotes, so a key that is supposed to be "apptDateTime"
is actually returned as " \"apptDateTime\""
. You can see this in the debug output you added: you don't add quotes around the keys or the values, but the output shows quotes anyway.
You can work around the bug by removing the wrapping quotes as follows, but it would be better to fix the function that returns unexpected data in the first place.
String key = removeQuotes(setJSONKeys.get(i));
String value = removeQuotes(setJSONValues.get(i))
requestMap.put(key, setJSONValues.get(i));
...
String removeQuotes(String key) {
key = key.trim();
key = key.substring(1, key.length() - 1); // remove quotes
return key.trim();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论