Json   发布时间:2022-04-22  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了fastjson常用方法大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
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。 */

大佬总结

以上是大佬教程为你收集整理的fastjson常用方法全部内容,希望文章能够帮你解决fastjson常用方法所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。