fastjson使用方法
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了fastjson使用方法相关的知识,希望对你有一定的参考价值。
1.fastjson介绍
fastjson是一个Java语言编写的JSON处理器, 由阿里巴巴公司开发
2.测试用到的类Person.java 和 Role.java
public class Person { private String pname; // 对指定属性不转换json @JSONField(serialize=false) private Role role; public String getPname() { return pname; } public void setPname(String pname) { this.pname = pname; } public Role getRole() { return role; } public void setRole(Role role) { this.role = role; } }
public class Role { private String rname; private Person person; public String getRname() { return rname; } public void setRname(String rname) { this.rname = rname; } public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; } }
3.测试Demo
public class Demo1 { // 对象转json @Test public void run1() { Customer c = new Customer(); c.setCust_id(20L); c.setCust_name("测试"); c.setCust_phone("120"); // 对象转为json String jsonString = JSON.toJSONString(c); // 打印结果 {"cust_id":20,"cust_name":"测试","cust_phone":"120"} System.out.println(jsonString); } // 集合转json @Test public void run2() { List<Customer> list = new ArrayList<Customer>(); Customer c = new Customer(); c.setCust_id(20L); c.setCust_name("测试"); c.setCust_phone("120"); Customer c2 = new Customer(); c2.setCust_id(30L); c2.setCust_name("测试2"); c2.setCust_phone("1200"); list.add(c); list.add(c2); // 集合转为json String jsonString = JSON.toJSONString(list); // 打印结果 // [{"cust_id":20,"cust_name":"测试","cust_phone":"120"},{"cust_id":30,"cust_name":"测试2","cust_phone":"1200"}] System.out.println(jsonString); } // fastjson禁止循环引用 @Test public void run3() { List<Customer> list = new ArrayList<Customer>(); Customer c = new Customer(); c.setCust_id(20L); c.setCust_name("测试"); c.setCust_phone("120"); list.add(c); list.add(c); // 集合转为json String jsonString = JSON.toJSONString(list); // 打印结果 // [{"cust_id":20,"cust_name":"测试","cust_phone":"120"},{"$ref":"$[0]"}] System.out.println(jsonString); // 集合转为json String jsonString1 = JSON.toJSONString(list, SerializerFeature.DisableCircularReferenceDetect); // 打印结果 // [{"cust_id":20,"cust_name":"测试","cust_phone":"120"},{"cust_id":20,"cust_name":"测试","cust_phone":"120"}] System.out.println(jsonString1); } // fastjson禁止循环引用 // Person和Role互相引用导致的死循环问题 @Test public void run4() { Person p = new Person(); p.setPname("小明"); Role r = new Role(); r.setRname("管理员"); p.setRole(r); r.setPerson(p); String jsonString = JSON.toJSONString(p, SerializerFeature.DisableCircularReferenceDetect); // 打印结果 如果不加注解 StackOverflowError // 打印结果 如果加上注解@JSONField(serialize=false) {"pname":"小明"} System.out.println(jsonString); } }
4.FastJsonUtils工具类
public class FastJsonUtil { /** * 将对象转成json串 * @param object * @return */ public static String toJSONString(Object object) { // DisableCircularReferenceDetect来禁止循环引用检测 return JSON.toJSONString(object, SerializerFeature.DisableCircularReferenceDetect); } /* * 输出json */ public static void write_json(HttpServletResponse response, String jsonString) { response.setContentType("application/json;utf-8"); response.setCharacterEncoding("UTF-8"); try { response.getWriter().print(jsonString); } catch (IOException e) { e.printStackTrace(); } } /** * ajax提交后回调的json字符串 * @return */ public static String ajaxResult(boolean success, String message) { Map map = new HashMap(); map.put("success", success);// 是否成功 map.put("message", message);// 文本消息 String json = JSON.toJSONString(map); return json; } /** * JSON串自动加前缀 * @param json 原json字符串 * @param prefix 前缀 * @return 加前缀后的字符串 */ public static String JsonFormatterAddPrefix(String json, String prefix, Map<String, Object> newmap) { if (newmap == null) { newmap = new HashMap(); } Map<String, Object> map = (Map) JSON.parse(json); for (String key : map.keySet()) { Object object = map.get(key); if (isEntity(object)) { String jsonString = JSON.toJSONString(object); JsonFormatterAddPrefix(jsonString, prefix + key + ".", newmap); } else { newmap.put(prefix + key, object); } } return JSON.toJSONString(newmap); } /** * 判断某对象是不是实体 * @param object * @return */ private static boolean isEntity(Object object) { if (object instanceof String) { return false; } if (object instanceof Integer) { return false; } if (object instanceof Long) { return false; } if (object instanceof java.math.BigDecimal) { return false; } if (object instanceof Date) { return false; } if (object instanceof java.util.Collection) { return false; } return true; } }
以上是关于fastjson使用方法的主要内容,如果未能解决你的问题,请参考以下文章
Fastjson 反序列化 Jndi 注入利用 JdbcRowSetImpl 链