加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

fastjson常用方法

发布时间:2020-12-16 19:55:32 所属栏目:百科 来源:网络整理
导读:public class FastJsonTest { public static void main(String[] args) { Customer obj = new Customer(); obj.setName("tuser"); obj.setPhone("01066010"); obj.setAddress("北京1区"); obj.setEmail("test@163.com"); System.out.println("序列化:把JavaBe
public class FastJsonTest { public static void main(String[] args) { Customer obj = new Customer(); obj.setName("tuser"); obj.setPhone("01066010"); obj.setAddress("北京1区"); obj.setEmail("test@163.com"); System.out.println("序列化:把JavaBean对象转化成JSON格式的文本"); System.out.println(JSON.toJSONString(obj));// 将JavaBean序列化为JSON文本。 System.out.println(JSON.toJSONString(obj,true));// 将JavaBean序列化为带格式的JSON文本 System.out.println(JSON.toJSONString(obj,SerializerFeature.WriteClassName));// 序列化时写入类型信息 System.out.println(JSON.toJSONString(obj,SerializerFeature.UseSingleQuotes));//使用单引号 List<Customer> list = new ArrayList<Customer>(); for (int i = 0; i < 5; i++) { Customer customer = new Customer(); customer.setName("tuser" + i); customer.setPhone("01066010" + i); customer.setAddress("北京" + i + "区"); customer.setEmail("t" + i + "gone@163.com"); list.add(customer); } System.out.println("序列化:把List集合对象转化成JSON格式的文本"); System.out.println(JSON.toJSONString(list));// 将JavaBean序列化为JSON文本。 System.out.println(JSON.toJSONString(list,true));// 将JavaBean序列化为带格式的JSON文本 System.out.println("序列化:将实体类中的某个字段解析"); // 某几个不进行解析:可以使用transient 关键字,来标记它为不需要的 SimplePropertyPreFilter filter = new SimplePropertyPreFilter( Customer.class,"name","phone"); System.out.println(JSON.toJSONString(obj,filter)); System.out.println("==========反序列化============"); String temp = JSON.toJSONString(obj,filter); Customer customer = JSON.parSEObject(temp,Customer.class); System.out.println("反序列化:" + customer.getName() + " " + customer.getPhone()); // 过滤哪些属性需要转换 SimplePropertyPreFilter filter2 = new SimplePropertyPreFilter( Customer.class,"phone"); String jsonCustomer = JSON.toJSONString(list,filter2); System.out.println(jsonCustomer); // 泛型的反序列化:Map<String,User> userMap = JSON.parSEObject(text,new // TypeReference<Map<String,User>>() {}); List<Customer> list2 = JSON.parSEObject(jsonCustomer,new TypeReference<List<Customer>>() { }); for (Customer c : list2) { System.out.println("反序列化:" + c.getName() + " " + c.getPhone()); } List<Customer> list3 = JSON.parseArray(jsonCustomer,Customer.class); for (Customer c : list3) { System.out.println("反序列化:" + c.getName() + " " + c.getPhone()); } System.out.println("=====其他======="); Date date = new Date(); System.out.println(JSON.toJSONString(date)); System.out.println(JSON.toJSONString(date,SerializerFeature.WriteDateUseDateFormat)); System.out.println(JSON.toJSONStringWithDateFormat(date,"yyyy-MM-dd HH:mm:ss.SSS")); } } /** * public static final Object parse(String text); * 把JSON文本parse为JSONObject或者JSONArray。 * * public static final JSONObject parSEObject(String text); * 把JSON文本parse成JSONObject 。 * * public static final <T> T parSEObject(String text,Class<T> clazz); * 把JSON文本parse为JavaBean。 * * public static final JSONArray parseArray(String text); * 把JSON文本parse成JSONArray。 * * public static final <T> List<T> parseArray(String text,Class<T> clazz); * 把JSON文本parse成JavaBean集合。 * * public static final String toJSONString(Object object); 将JavaBean序列化为JSON文本。 * * public static final String toJSONString(Object object,boolean prettyFormat); * 将JavaBean序列化为带格式的JSON文本。 * * public static final Object toJSON(Object javaObject); * 将JavaBean转换为JSONObject或者JSONArray。 */

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读