import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.serializer.SerializerFeature; import com.alibaba.fastjson.serializer.SimplePropertyPreFilter; import java.util.ArrayList; import java.util.List; public class CqUtil { //将obj转换成JSONArray public JSONArray ObjectToJSONArray(Object obj, String ...filters) { SimplePropertyPreFilter spp = new SimplePropertyPreFilter(); for (String filter : filters) { spp.getExcludes().add(filter); } String jsonString = JSONObject.toJSONString(obj, spp, SerializerFeature.DisableCircularReferenceDetect); return JSONArray.parseArray(jsonString); } //将Object转换成JSONObject public JSONObject ObjectToJSONObject(Object obj, String ...filters) { SimplePropertyPreFilter spp = new SimplePropertyPreFilter(); for (String filter : filters) { spp.getExcludes().add(filter); } String jsonString = JSONObject.toJSONString(obj, spp, SerializerFeature.DisableCircularReferenceDetect); return JSONObject.parseObject(jsonString); } //JSONArray转换成JSONObject public JSONObject JsonArrayToJsonObject(JSONArray jo) throws Exception{ JSONObject ret = new JSONObject(); ret.put("data", jo == null ? "[]".getBytes() : jo.toJSONString().getBytes("UTF-8")); return ret; } //JSONObject转换成JSONArray public JSONArray JSONObjectToJSONArray(JSONObject object){ JSONArray array = new JSONArray(); String strJson=object.toJSONString(); array.add(strJson); return array; } //JSONArray转换成List public List JSONArrayToList(JSONArray jsonArray){ List list = new ArrayList(); jsonArray.forEach(array->{ list.add(array); }); return list; } }
自己封装了些FastJson中格式转换的方法
最新推荐文章于 2023-08-29 09:54:13 发布