com.alibaba的fastjson使用(持续添加)

1.JSON换实体类T

1 //json是JSONObject类型,将json转成实体类T
2 T tt = JSONObject.parseObject(json.toJSONString(), T.class);

2.将json某字段转成list

1 //先转成JSONArray,再转成list<T>
2 JSONArray jsonArray = json.getJSONArray("listKey");
3 List<T> tList = (List<T>) JSONArray.parseArray(jsonArray .toString(), T.class);

 3.将json转成map

1 //获取传过来的map
2 JSONObject paramsJson=***3 Map<String, String> params = JSONObject.parseObject(paramsJson.toJSONString(), new TypeReference<Map<String, String>>() {});

 4.字符串转JSONAlibaba

JSONObject json = JSON.parseObject(paramData);

 5.将list<Bean>转成JSON字符串

JSONObject.toJSONString(userList);

6. 将list<Bean>转成list<Json>

 1 public static <T> List<JSONObject> listConvertJSONObject(List<T> list) {
 2     // list为外部传进来的list集合
 3     List<JSONObject> jsonObjectList = new ArrayList<JSONObject>();
 4     if (CollectionUtils.isNotEmpty(list)) {
 5         list.forEach(item -> {
 6             jsonObjectList.add(JSONObject.parseObject(JSONObject.toJSONString(item)));
 7         });
 8     }
 9     return jsonObjectList;
10 }

 7.将list<Bean>转成list<Bean2>

1 public static <T, U> List<U> listBean2listOtherBean(List<T> list, Class<U> clazz) {
2     JSONArray array = JSONArray.parseArray(JSON.toJSONString(list));
3     List<U> tList = (List<U>) JSONArray.parseArray(array.toString(), clazz);
4     return tList;
5 }

 

posted @ 2020-05-12 15:12  龙谷情Sinoam  阅读(1129)  评论(0编辑  收藏  举报
Smiley face