【问题标题】:Query on Java Jackson Implementation查询Java Jackson实现
【发布时间】:2023-01-12 07:25:35
【问题描述】:

我有一个以下格式的 json 文件:

{   
   "resources" : {     
        "foo" : {       
          "value" : "test"      
         },     
        "bar" : {       
          "value" : "test"     
         }    
   } 
}

而且,我创建了它的 JAVA 类,如下所示:

package org.example.model;

import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.HashMap;
import java.util.List;

public class JsonTwoJavaFileModel {
   @JsonProperty("resources")
   private HashMap<String, HashMap<String, List<String>>>  stringListHashMap;
}

但是,在阅读时出现以下错误:

`Exception in thread "main" com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `java.util.ArrayList` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('test')  at [Source: (File); line: 4, column: 17] (through reference chain: org.example.model.JsonTwoJavaFileModel["resources"]->java.util.HashMap["offer1"]->java.util.HashMap["value"])`

不确定,如何为这种类型的嵌套 JSON 创建 Java 类。任何指针来解决它?

还尝试像下面这样创建 Java 类:

`package org.example.model;

import com.fasterxml.jackson.annotation.JsonProperty;    
import java.util.HashMap;
import java.util.List;

public class JsonTwoJavaFileModel {
   @JsonProperty("resources")
   private HashMap<List<String>, HashMap<String, List<String>>> stringListHashMap;
}`

但是这两种方法都没有运气。

【问题讨论】:

  • 您的数据结构有一个 List,但没有对应的 JSON 数组。
  • 如果我理解正确,那么资源是一把钥匙,它有多个值。它的每个值都是地图类型。

标签: java jackson-databind


【解决方案1】:

尝试这个 :

@JsonProperty("resources")
private HashMap<String, HashMap<String, HashMap<String, String>>> stringListHashMap;

【讨论】:

  • 我认为您需要另一个嵌套地图。
  • 谢谢。我更新了答案
猜你喜欢
  • 2013-09-24
  • 1970-01-01
  • 2019-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-23
  • 2013-12-27
  • 1970-01-01
相关资源
最近更新 更多