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。
   
   */
  
 
