Unrecognized field异常,原因是objectMapper.writeValueAsString的输出格式不正确。

huangapple go评论84阅读模式
英文:

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&lt;Object&gt; setJSONValues = new ArrayList&lt;Object&gt;(Arrays.asList(requestObj));
List&lt;String&gt; setJSONKeys = apiUtility.readJSONKeys(new  File(&quot;ABC.csv&quot;));
HashMap&lt;String, Object&gt; requestMap = new HashMap&lt;String, Object&gt;();
 if (setJSONKeys.size() == setJSONValues.size()) {
     for (int i = 0; i &lt; 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 :
{" &quot;apptDateTime&quot;":"&quot;2019-03-19 10:00:00&quot;","&quot;meter&quot;":"&quot;8682&quot;"

Adding Hashmap O/P :

for (String s:requestMap.keySet())
  System.out.println(&quot;Key is &quot;+s+&quot;Value is &quot;+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"));

它返回的键和值被双引号包裹,所以本应是 &quot;apptDateTime&quot; 的键实际上被返回为 &quot; \&quot;apptDateTime\&quot;&quot;。您可以在您添加的调试输出中看到这一点:您并未在键或值周围添加引号,但输出仍然显示了引号。

您可以通过以下方式绕过此错误,但最好修复首次返回意外数据的函数。

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&lt;String&gt; setJSONKeys = apiUtility.readJSONKeys(new  File(&quot;ABC.csv&quot;));

It is returning keys and values wrapped in double quotes, so a key that is supposed to be &quot;apptDateTime&quot; is actually returned as &quot; \&quot;apptDateTime\&quot;&quot;. 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();
}

huangapple
  • 本文由 发表于 2020年4月9日 04:44:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/61109699.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定