日期:2014-05-16 浏览次数:20495 次
最近在做项目,需要用到json和java之间的交互,本人对json不是特别精通,于是开始搜索一些java平台的json类库。
发现了google的gson,带着一些好奇心,我开始使用了gson。
经过比较,gson和其他现有java json类库最大的不同时gson需要序列化得实体类不需要使用annotation来标识需要序列化得字段,
同时gson又可以通过使用annotation来灵活配置需要序列化的字段。
还有一款fastjson也不错执行性能更优,他们都是不依赖其他类库的,但是个人还是习惯用gson,呵呵。
不废话上代码看看吧
这是一个用户实体,里面包含了基本类型,数组和list和map类型,
都是大家常用的。
public class User { private int id; private String name; private boolean isAdmin; private String[] tels; private List<Bank> banks; private Map doSome; public User() { // TODO Auto-generated constructor stub } public User(int id, String name, boolean isAdmin, String[] tels, List<Bank> banks, Map doSome) { super(); this.id = id; this.name = name; this.isAdmin = isAdmin; this.tels = tels; this.banks = banks; this.doSome = doSome; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public boolean isAdmin() { return isAdmin; } public void setAdmin(boolean isAdmin) { this.isAdmin = isAdmin; } public String[] getTels() { return tels; } public void setTels(String[] tels) { this.tels = tels; } public List<Bank> getBanks() { return banks; } public void setBanks(List<Bank> banks) { this.banks = banks; } public Map getDoSome() { return doSome; } public void setDoSome(Map doSome) { this.doSome = doSome; } @Override public String toString() { return "id:"+id+",name:"+name+",isAdmin:"+ isAdmin+",tels:"+tels[0]+",banks:"+banks.get(0).getBankName()+"," + banks.get(1).getBankName() + ",10点:" + doSome.get("10点"); } }这个是user里的banks俺其实就只有这2张。
public class Bank { private Integer id; private String bankName; public Bank() { // TODO Auto-generated constructor stub } public Bank(Integer id, String bankName) { super(); this.id = id; this.bankName = bankName; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getBankName() { return bankName; } public void setBankName(String bankName) { this.bankName = bankName; } }
/** * 测试gson * @author jelly * */ public class Test { public static void main(String[] args) { Gson gson = new Gson(); //java复杂对象转json Bank b1 = new Bank(1,"中国银行卡"); Bank b2 = new Bank(2,"工行银行卡"); List<Bank> banks = new ArrayList<Bank>(); banks.add(b1);banks.add(b2); Map map = new HashMap(); map.put("10点", "出去卖萌!"); User user1 = new User(1,"张三",false,new String[]{"130000000","150"},banks,map); String toString = gson.toJson(user1); System.out.println(toString); System.out.println("\n//--------------华丽的分割线--------------//\n"); //json转java对象 String json = "{'id':1,'name':'张三','isAdmin':false,'tels':['130','150'],'banks':[{'id':1,'bankName':'中国银行卡'},{'id':2,'bankName':'工行银行卡'}],'doSome':{'10点':'出去卖萌!'}}"; User u = gson.fromJson(json, User.class); System.out.println(u); } }
输出结果:
{"id":1,"name":"张三","isAdmin":false,"tels":["130000000","150"],"banks":[{"id":1,"bankName":"中国银行卡"},{"id":2,"bankName":"工行银行卡"}],"doSome":{"10点":"出去卖萌!"}} //--------------华丽的分割线--------------// id:1,name:张三,isAdmin:false,tels:130,banks:中国