【问题标题】:Jackson unable to convert json to list of maps杰克逊无法将 json 转换为地图列表
【发布时间】:2018-09-20 12:56:30
【问题描述】:

在我的 spring-boot 项目中,我有一个控制器

@RequestMapping(path = { "/multiCommunication" }, consumes = {
        MediaType.APPLICATION_JSON_VALUE }, method = RequestMethod.POST)
ResponseEntity<Object> multiCommunication(ArrayList<HashMap<String, String>> listOfInput){ //code}

我无法访问该列表,因为 Jackson 无法提出我的请求。

请求:

[ { “类型”:“HPMRE”, "pipFirstName": "ABC" }, { “类型”:“HPMRE”, “pipFirstName”:“XYZ” } ]

JSON 有问题吗?

我尝试制作一个模型类,它的变量类型为ArrayList&lt;HashMap&lt;String, String&gt;&gt; 仍然杰克逊无法转换它。

【问题讨论】:

  • 您是否收到 ClassCastException?分享确切的错误和您收到错误的代码行
  • 嗨 Vineeth,即使我只是打印列表,列表也是空的。
  • 我认为您无法投射请求....如果列表为空,您希望如何投射?
  • Vineeth, JSON to list casting就是我所说的
  • 尝试获取此字符串并使用 ObjectMapper 将其解析为列表。

标签: java json spring-boot jackson


【解决方案1】:

试试这个

@RequestMapping(path = { "/multiCommunication" }, consumes = { MediaType.APPLICATION_JSON_VALUE }, method = RequestMethod.POST) ResponseEntity<Object> multiCommunication(ArrayList<LinkedHashMap<String, String>> listOfInput){

【讨论】:

    【解决方案2】:

    尝试将 HashMap 更改为 LinkedHashMap:

    @RequestMapping(path = { "/multiCommunication" }, consumes = {
            MediaType.APPLICATION_JSON_VALUE }, method = RequestMethod.POST)
    ResponseEntity<Object> multiCommunication(ArrayList<LinkedHashMap<String, String>> listOfInput){ 
    //code
    }
    

    【讨论】:

      猜你喜欢
      • 2022-01-03
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多