map转对象方法:
1.依赖阿里 fastjson:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.46.sec01</version>
</dependency>
2:
//map 转对象
public static <T> T parseMap2Object(Map<String, Object> paramMap, Class<T> cls) {
return JSONObject.parseObject(JSONObject.toJSONString(paramMap), cls);
}
cookic字节转字符处理:
Cookie cookie = null;
try {
cookie = new Cookie("cartItem", URLEncoder.encode(json, "utf-8")); //转utf-8字节
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
cookie.setMaxAge(60 * 60 * 24 * 7);
cookie.setPath(request.getContextPath() + "/");//路径
response.addCookie(cookie);
String value = cookie.getValue();
value = URLDecoder.decode(value); //utf-8字节转回字符串
Map转对象<泛型>
public static void main(String[] args) { Map<String,Object> map =new HashMap<>(); map.put("orderType","已完成" );map.put("orderMoney",34.38 );map.put("orderNum","0002-000220201016-000001" ); map.put("dealType","销售" );map.put("payTypeName","微信" );map.put("payType", 3); SalesOrder tobean = tobean(map, SalesOrder.class); System.out.println(tobean); } public static <T> T tobean(Map<String, Object> map, Class<T> clazz) { try { T bean = clazz.newInstance(); BeanUtils.populate(bean, map); return bean; } catch (Exception e) { throw new RuntimeException(e); } }