标签:
实体类
User.java
1 package com.test.fastjson.entity; 2 3 import java.util.Date; 4 5 public class User { 6 private Long id; 7 /** 8 * 登录名 9 */ 10 private String loginName; 11 /** 12 * 用户名 13 */ 14 private String userName; 15 /** 16 * 手机号码 17 */ 18 private String cellNO; 19 20 /** 21 * password 22 */ 23 private String password; 24 /** 25 * update时间 26 */ 27 private Date updateDate; 28 29 private String sex; 30 31 private Classes classes; 32 33 /** 34 * 是否可用 35 * [JPA没有boolean类型,在get/set方法中修改. 持久导的实体属性使用Character类型,则领域层可直接使用boolean类型。] 36 */ 37 private Character enabled; 38 39 // setter/getter 40 41 public Boolean getEnabled() { 42 if (enabled == null) 43 return null; 44 return enabled == ‘Y‘ ? Boolean.TRUE : Boolean.FALSE; 45 } 46 47 public void setEnabled(Boolean enabled) { 48 if (enabled == null) { 49 this.enabled = null; 50 } else { 51 this.enabled = enabled == true ? ‘Y‘ : ‘N‘; 52 } 53 } 54 55 public String getLoginName() { 56 return loginName; 57 } 58 59 public String getSex() { 60 return sex; 61 } 62 63 public void setSex(String sex) { 64 this.sex = sex; 65 } 66 67 public void setLoginName(String loginName) { 68 this.loginName = loginName; 69 } 70 71 public String getUserName() { 72 return userName; 73 } 74 75 public void setUserName(String userName) { 76 this.userName = userName; 77 } 78 79 public String getCellNO() { 80 return cellNO; 81 } 82 83 public void setCellNO(String cellNO) { 84 this.cellNO = cellNO; 85 } 86 87 public Long getId() { 88 return id; 89 } 90 91 public void setId(Long id) { 92 this.id = id; 93 } 94 95 public String getPassword() { 96 return password; 97 } 98 99 public void setPassword(String password) { 100 this.password = password; 101 } 102 103 public Date getUpdateDate() { 104 return updateDate; 105 } 106 107 public void setUpdateDate(Date updateDate) { 108 this.updateDate = updateDate; 109 } 110 111 public Classes getClasses() { 112 return classes; 113 } 114 115 public void setClasses(Classes classes) { 116 this.classes = classes; 117 } 118 }
classes.java
1 package com.test.fastjson.entity; 2 3 import java.util.List; 4 5 public class Classes { 6 7 private String name; 8 private List<User> u; 9 10 public String getName() { 11 return name; 12 } 13 public void setName(String name) { 14 this.name = name; 15 } 16 public List<User> getU() { 17 return u; 18 } 19 public void setU(List<User> u) { 20 this.u = u; 21 } 22 23 24 }
testClient
1 package com.test.fastjson; 2 3 import java.util.Date; 4 5 import com.alibaba.fastjson.JSON; 6 import com.alibaba.fastjson.serializer.SerializerFeature; 7 import com.test.fastjson.entity.Classes; 8 import com.test.fastjson.entity.User; 9 10 public class testFastjson { 11 12 public static void main(String[] args) { 13 Classes c=new Classes(); 14 c.setName("class one"); 15 16 User u=new User(); 17 u.setCellNO("1555555555"); 18 u.setId(232323L); 19 u.setUpdateDate(new Date()); 20 u.setClasses(c); 21 22 //obj2str 序列化 23 String str=JSON.toJSONString(u); 24 System.out.println(str); 25 //str2obj 反序列化: 26 User u2=JSON.parseObject(str, User.class); 27 // Object o=JSON.parse(str); 28 System.out.println("cellNO= "+u2.getCellNO()+" UpdateDate= "+u2.getUpdateDate()+" class= "+u2.getClass().getName()); 29 30 } 31 32 33 }
执行效果
{"cellNO":"1555555555","classes":{"name":"class one"},"id":232323,"updateDate":1421047634650}
cellNO= 1555555555 UpdateDate= Mon Jan 12 15:27:14 CST 2015 class= com.test.fastjson.entity.User
标签:
原文地址:http://www.cnblogs.com/redcoatjk/p/4218504.html