fastjson使用示例
一、fastjson
一款Json处理工具包,包括“序列化”和“反序列化”两部分,它具备如下特征:
速度最快,测试表明,fastjson具有极快的性能,超越任其他的Java Json parser。包括自称最快的JackJson;
功能强大,完全支持Java Bean、集合、Map、日期、Enum,支持范型,支持自省。
二、api 接口demo
①、复杂的Object 转为 json
JSON.toJSONString(Object o)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class ContentData { 2 3 private List<ContentRes> contents; 4 5 public List<ContentRes> getContents() { 6 return contents; 7 } 8 9 public void setContents(List<ContentRes> contents) { 10 this.contents = contents; 11 } 12 13 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class ContentRes { 2 3 private boolean success; 4 5 private String msg; 6 7 private Long tatal; 8 9 private List<ContentDO> data; 10 11 public boolean isSuccess() { 12 return success; 13 } 14 15 public String getMsg() { 16 return msg; 17 } 18 19 public Long getTatal() { 20 return tatal; 21 } 22 23 public List<ContentDO> getData() { 24 return data; 25 } 26 27 public void setSuccess(boolean success) { 28 this.success = success; 29 } 30 31 public void setMsg(String msg) { 32 this.msg = msg; 33 } 34 35 public void setTatal(Long tatal) { 36 this.tatal = tatal; 37 } 38 39 public void setData(List<ContentDO> data) { 40 this.data = data; 41 } 42 43 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class ContentDO { 2 3 private String id; 4 5 private String title; 6 7 private String url; 8 9 public String getId() { 10 return id; 11 } 12 13 public void setId(String id) { 14 this.id = id; 15 } 16 17 public String getTitle() { 18 return title; 19 } 20 21 public void setTitle(String title) { 22 this.title = title; 23 } 24 25 public String getUrl() { 26 return url; 27 } 28 29 public void setUrl(String url) { 30 this.url = url; 31 } 32 33 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class Client { 2 3 public static void main(String[] args) { 4 String js = JSON.toJSONString(initContentData()); 5 System.out.println(js); 6 7 } 8 9 static ContentData initContentData() { 10 ContentData c = new ContentData(); 11 c.setContents(initContentRess()); 12 return c; 13 } 14 15 static ContentRes initContentRes() { 16 17 ContentRes c = new ContentRes(); 18 c.setData(initContentDOs()); 19 c.setMsg("msg"); 20 c.setSuccess(true); 21 c.setTatal(100L); 22 return c; 23 } 24 25 static List<ContentRes> initContentRess() { 26 List<ContentRes> res = new ArrayList<ContentRes>(); 27 res.add(initContentRes()); 28 return res; 29 } 30 31 static List<ContentDO> initContentDOs() { 32 List<ContentDO> res = new ArrayList<ContentDO>(); 33 for (int i = 0; i < 3; i++) { 34 ContentDO c = new ContentDO(); 35 c.setId("id" + i); 36 c.setTitle("title" + i); 37 c.setUrl("url" + i); 38 res.add(c); 39 } 40 return res; 41 } 42 }
结果 :
{ "contents": [ { "data": [ { "id": "id0", "title": "title0", "url": "url0" }, { "id": "id1", "title": "title1", "url": "url1" }, { "id": "id2", "title": "title2", "url": "url2" } ], "msg": "msg", "success": true, "tatal": 100 } ] }
②、将json 数据转为object 对象
JSON.parseObject(String text, Class<T> clazz)
public static void main(String[] args) { String js = JSON.toJSONString(initContentData()); System.out.println(js); ContentData obj = JSON.parseObject(js, ContentData.class); System.out.println(obj); js = JSON.toJSONString(initContentData2()); System.out.println(js); }
结果 :
三、近来项目,老是遇到一些细节,有必要在此提一下。
①、List 转为json 数据的格式。[...]
[
{},
{},
{}
]
demo,可以见上诉Object转为json数据
②、Array 转为json 数据的格式 。 同上
四、推荐JSON格式化的工具
http://qqe2.com/
http://www.bejson.com/