SysPluginTypeDef sysPluginTypeDef = new SysPluginTypeDef();
sysPluginTypeDef.setName("xxx");
List<SysPluginTypeDef> map = sysPluginTypeDefService.selectSysPluginTypeDefList(sysPluginTypeDef);
System.out.println(map);
打印的数据是下面这个
[{"id":"515aa791-abf0-11eb-8581-901b0ec7714e","name":"PLC-KEP","deleted":0,"createTime":"Mon May 03 17:17:02 CST 2021"}]
怎样将这个结果转换为Map<string,object>形式的呢,强转会报错。
ps: 在entity设置了
ToStringStyle.JSON_STYLE 所以打印结果是上面那种
public String toString() {
return new ToStringBuilder(this,ToStringStyle.JSON_STYLE)
.append("id", getId())
.append("name", getName())
.append("deleted", getDeleted())
.append("createTime", getCreateTime())
.toString();
}
Map<string,object> 中key是存的什么?id?还是name,value是什么这个对象吗
map是键值对,你这里list转换完之后是一个数组,当然是不行的。
List<Map<String,Object>> list= JSON.parseArray(JSON.toJSONString(sysPluginTypeDef);
这样试试能不能转换成功
List<SysPluginTypeDef> map
这个对象再怎么转 也只能转成一个集合,肯定不能转成Map。如果你是想转成List<Map>是可以的。
List<Map> newMap= JSONUtil.toList(JSONUtil.parseArray(map.toString()), Map.class);
import cn.hutool.json.JSONUtil;这个是工具类。
Map<String,SysPluginTypeDef> result = map.stream().collect(Collectors.toMap(SysPluginTypeDef::getId,a->a)); java8 stream 试一下
我想到了一个办法:
Map<String,Object> systypedef = new HashMap<String,Object>();
List<SysPluginTypeDef> map = sysPluginTypeDefService.selectSysPluginTypeDefList(sysPluginTypeDef);
for(SysPluginTypeDef s:map)
{
systypedef.put(s.getId(),s.getName());
}
System.out.println(map);
这样可以解决