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中格式轉換的方法
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.