Json工具类之"Json格式转换总结"

一、HashMap转Json

HashMap<String, String> oldContentMap = new HashMap<>();
oldContentMap.put("id",oldProduct.getProductId().toString());
oldContentMap.put("productTitle",oldProduct.getProductTitle());
String s = JSONUtil.toJsonStr(oldContentMap);

 

二、JSON 转List -》String类型

String post = "{\"code\":\"200\",\"msg\":\"success\",\"data\":[\"Yinqing\",\"ArtCreativity\"]}";

// 方法1[常用]:获取data中的数据转为List
List<String> stringList1 = JSON.parseObject(post).getJSONArray("data").toJavaList(String.class);

 

三、JSON转List -》 对象类型

String post = "{\"code\":\"200\",\"msg\":\"success\",\"data\":[{\"userId\":\"1\",\"userName\":\"wzw\"},{\"userId\":\"2\",\"userName\":\"zrf\"}]}";

// 方法1[常用]:获取data中的数据转为List
List<User> stringList1 = JSON.parseObject(post).getJSONArray("data").toJavaList(User.class);
stringList1.forEach(item -> System.out.println("item.getUserId() = " + item.getUserId()));

 

posted @ 2023-03-23 14:29  骚哥  阅读(163)  评论(0编辑  收藏  举报