功能:
1. HashMap<Object,Object> 转成对象 AlertDeviceInfo
2 List<HashMap<Object,Object>> 转为 List<AlertDeviceInfo>
核心代码:
package com.whm.bigdata;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import lombok.extern.slf4j.Slf4j;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
@Slf4j
public class testA {
public static void main(String[] args) {
List<HashMap<Object, Object>> list = new ArrayList<>();
HashMap<Object, Object> map = new HashMap<>();
map.put("devUid", 201);
map.put("devLogIds", Arrays.asList("1"));
map.put("devAttackResult", 1);
map.put("vendor", "sangfor");
map.put("productVer", "AF8.0.1");
map.put("originProductType", "SIP");
map.put("originProductVer", "SIP5.0.58");
map.put("deviceId", "HGFEDCBA");
map.put("productType", "AF");
map.put("manage", "ABCDEFGH");
map.put("devLogCount", 10);
list.add(map);
List<AlertDeviceInfo> hostInfos = list.stream()
.map(entity -> JSONUtil.toBean(JSONUtil.parseObj(entity), AlertDeviceInfo.class)).collect(Collectors.toList());
log.info("--list: {}", hostInfos);
System.out.println(get(list));
AlertDeviceInfo alertDeviceInfo = JSONUtil.toBean(JSONUtil.parseObj(map), AlertDeviceInfo.class);
log.info("--map: {}", alertDeviceInfo);
}
private static List<AlertDeviceInfo> get(Object object) {
List<AlertDeviceInfo> alertDeviceInfos = new ArrayList<>();
try {
alertDeviceInfos = JsonUtil.getObjectMapper().convertValue(
object,
new TypeReference<List<AlertDeviceInfo>>() {
}
);
} catch (Exception e) {
System.out.println("转换错误");
}
return alertDeviceInfos;
}
}
工具包
package com.whm.bigdata;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import javax.validation.constraints.NotNull;
public class JsonUtil {
private static ObjectMapper OBJECT_MAPPER;
private JsonUtil() {
}
@NotNull
public static ObjectMapper getObjectMapper() {
if (OBJECT_MAPPER == null) {
ObjectMapper localObjectMapper = new ObjectMapper();
localObjectMapper.enable(new JsonParser.Feature[]{JsonParser.Feature.ALLOW_COMMENTS});
localObjectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
localObjectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
OBJECT_MAPPER = localObjectMapper;
}
return OBJECT_MAPPER;
}
}
实体类,如果要控制输出属性,就展示要输出的,比如上面11个属性,只要2个,写一个输出对象,就只有2个属性的,我这根据有个需求,不同公司返回该对象的不同字段展示文章来源:https://www.toymoban.com/news/detail-660219.html
package com.whm.bigdata;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
@AllArgsConstructor
@NoArgsConstructor
@Data
public class AlertDeviceInfo {
private String deviceId;
private String manage;
// private String originProductType;
//
// private String originProductVer;
//
// private String vendor;
//
// private String productType;
//
// private String productVer;
//
// @JsonProperty("devUId")
// private Integer devUid;
//
// private List<String> devLogIds;
//
// private Integer devLogCount;
//
// private Integer devAttackResult;
}
JSON + JAVA 8 Stream处理该问题文章来源地址https://www.toymoban.com/news/detail-660219.html
到了这里,关于Java 8 中Map转成对象的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!