繁体   English   中英

杰克逊将json转换为String和列表的映射

[英]Jackson to convert a json into a map of String and a list

我有一个像下面的json

{
  "department": [
    {
      "status": "active",
      "count": "100"
    },
    {
      "status": "active",
      "count": "300"
    }
  ],
  "finance": [
    {
      "status": "inactive",
      "count": "500"
    },
    {
      "status": "active",
      "count": "450"
    }
  ]
}

我的对象如下

class CurrentInfo
{
private String status;
private int count;
//getters and setters
}

我想将此JSON转换为Map<String,List<CurrentInfo>> 我可以使用以下内容转换单个节点

ObjectMapper mapper = new ObjectMapper();
CurrentInfo currentinfo = mapper.readValue(jsonString, CurrentInfo.class);

有没有一种方法可以将前面提到的JSON直接转换为Map<String,List<CurrentInfo>>

是的,您可以,并且根据上面的JSON countString类型不是int

class CurrentInfo {
    private String status;
    private String count;
    //getters and setters
}

使用TypeReference将json字符串转换为java对象

Map<String, List<CurrentInfo>> currentInfo = mapper.readValue(
        jsonString,
        new TypeReference<Map<String, List<CurrentInfo>>>() {});

暂无
暂无

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

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