jackson基本使用库

    技术2025-06-27  20

       提供json与对象之间的基本转换

      

    public class JsonUtil { /** * 将对象转换为json字符串 * * @param obj * @return * @throws Exception */ public static String obj2string(Object obj) { StringWriter sw = new StringWriter(); ObjectMapper mapper = new ObjectMapper(); try { mapper.writeValue(sw, obj); } catch (Exception e) { } return sw.toString(); } /** * 将字符串转list对象 * * @param <T> * @param jsonStr * @param cls * @return */ public static <T> List<T> str2list(String jsonStr, Class<T> cls) { ObjectMapper mapper = new ObjectMapper(); List<T> objList = null; try { JavaType t = mapper.getTypeFactory().constructParametricType( List.class, cls); objList = mapper.readValue(jsonStr, t); } catch (Exception e) { } return objList; } /** * 将字符串转为对象 * * @param <T> * @param jsonStr * @param cls * @return */ public static <T> T str2obj(String jsonStr, Class<T> cls) { ObjectMapper mapper = new ObjectMapper(); T obj = null; try { obj = mapper.readValue(jsonStr, cls); } catch (Exception e) { } return obj; } /** * 将字符串转为Page对象 * * @param <T> * @param jsonStr * @param cls * @return */ public static <T> Page<T> str2page(String jsonStr, Class<T> cls) { ObjectMapper mapper = new ObjectMapper(); Page<T> objList = null; try { JavaType t = mapper.getTypeFactory().constructParametricType( Page.class, cls); objList = mapper.readValue(jsonStr, t); } catch (Exception e) { } return objList; } /** * 将字符串转为json节点 * @param jsonStr * @return */ public static JsonNode str2node(String jsonStr) { ObjectMapper mapper = new ObjectMapper(); try { return mapper.readTree(jsonStr); } catch (Exception e) { e.printStackTrace(); } return null; }

    使用方式:

    public static void main(String[] args) { List<TestObj> list = new ArrayList<TestObj>(); String listStr = JsonUtil.obj2string(list); list = JsonUtil.str2list(listStr, TestObj.class); }

    下面代码因为博客原因不能删除,不可用

    /** * @param args */ public static void main(String[] args) { List<TestPojo> list = new ArrayList<TestPojo>(); for(int i = 0; i < 10; i++) { TestPojo pojo1 = new TestPojo(); pojo1.setName(i + "11"); pojo1.setSex(i + "22"); list.add(pojo1); } ObjectMapper mapper = new ObjectMapper(); StringWriter sw = new StringWriter(); try { mapper.writeValue(sw, list); } catch (JsonGenerationException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } List<TestPojo> list2 = new ArrayList<TestPojo>(); try { JSONArray array = new JSONArray(sw.toString()); for(int i = 0; i < array.length(); i++) { TestPojo pojoReverse = mapper.readValue(array.getString(i), TestPojo.class); System.out.println(pojoReverse); list2.add(pojoReverse); } } catch (JsonParseException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("list size = " + list.size()); System.out.println("list2 size = " + list2.size()); }

     

     

     

     

     

    最新回复(0)