import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.eeesys.xzjs.entity.Part;
import com.eeesys.xzjs.entity.User;
public class testFastJson {
public static void main(String[] args) {
User user=new User();
user.setId(1l);
user.setName("zhang");
user.setEmail("123@q");
user.setCompany("\"jsdfl\"");
user.setDate(new Date());
Part part1=new Part(1l, "p1");
Part part2=new Part(2l, "p2");
user.getParts().add(part1);
user.getParts().add(part2);
//类的序列化
String userJson=JSON.toJSONString(user);
System.out.println(userJson);
//参数可变长
String userJson2=JSON.toJSONString(user,SerializerFeature.DisableCheckSpecialChar, SerializerFeature.WriteDateUseDateFormat );
System.out.println(userJson2);
//日期格式化
String dateStr=JSON.toJSONStringWithDateFormat(user, "yyyy-MM-dd",SerializerFeature.UseSingleQuotes , SerializerFeature.DisableCheckSpecialChar);
System.out.println(dateStr);
//其它类型的序列化
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("username", "zhangsan");
map.put("age", 24);
map.put("sex", "男");
//map集合
HashMap<String, Object> temp = new HashMap<String, Object>();
temp.put("name", "xiaohong");
temp.put("age", "23");
map.put("girlInfo", temp);
//list集合
List<String> list = new ArrayList<String>();
list.add("爬山");
list.add("骑车");
list.add("旅游");
map.put("hobby", list);
String jsonString = JSON.toJSONString(map);
System.out.println("JSON=" + jsonString);
// DisableCheckSpecialChar:一个对象的字符串属性中如果有特殊字符如双引号,将会在转成json时带有反斜杠转移符。如果不需要转义,可以使用这个属性。默认为false
// UseSingleQuotes 输出单引号
// QuoteFieldNames———-输出key时是否使用双引号,默认为true
// WriteMapNullValue——–是否输出值为null的字段,默认为false
// WriteNullNumberAsZero—-数值字段如果为null,输出为0,而非null
// WriteNullListAsEmpty—–List字段如果为null,输出为[],而非null
// WriteNullStringAsEmpty—字符类型字段如果为null,输出为”“,而非null 测试不通过
// WriteNullBooleanAsFalse–Boolean字段如果为null,输出为false,而非null
// WriteDateUseDateFormat 日期类型的转换
// BrowserCompatible 浏览器和设备兼容
// @JSONField(name="ID") 在字段上加上注解,可使序列化时字段名称改为注解名称
//反序列化
JSONObject jObj=JSONObject.parseObject(dateStr);
String pStr=jObj.get("parts").toString();
//json to array
List pList=JSON.parseArray(pStr);
//json to Map
Map jMap=JSON.parseObject(pList.get(0).toString());
System.out.println(jMap.get("name"));
//类的序列化,支持泛型
String json="{\"part\":{\"name\":\"张三\",\"id\":123}}";
Map<String, User> uMap = JSON.parseObject(json, new TypeReference<Map<String, User>>(){});
System.out.println(uMap.get("part"));
}
}
//user类
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private String name;
private String company;
private String email;
private Date date;
private List<Part> parts = new ArrayList<Part>();
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public List<Part> getParts() {
return parts;
}
public void setParts(List<Part> parts) {
this.parts = parts;
}
}
//part类
import java.io.Serializable;
public class Part implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Part(Long id, String name) {
super();
this.id = id;
this.name = name;
}
}