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, "name", "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。
 */
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值