专注技术,奋斗不止

程序员-小倪的个人博客

fastjson 使用实例

Entity

publicclassUser {
    privateLong id;
    privateString name;
  
    publicLong getId() { returnid; }
    publicvoidsetId(Long id) { this.id = id; }
  
    publicString getName() { returnname; }
    publicvoidsetName(String name) { this.name = name; }
}
publicclassGroup {
    privateLong id;
    privateString name;
    privateList<User> users = newArrayList<User>();
  
    publicLong getId() { returnid; }
    publicvoidsetId(Long id) { this.id = id; }
  
    publicString getName() { returnname; }
    publicvoidsetName(String name) { this.name = name; }
  
    publicList<User> getUsers() { returnusers; }
    publicvoidsetUsers(List<User> users) { this.users = users; }
}

Encode

importcom.alibaba.fastjson.JSON;
  
Group group = newGroup();
group.setId(0L);
group.setName("admin");
  
User guestUser = newUser();
guestUser.setId(2L);
guestUser.setName("guest");
  
User rootUser = newUser();
rootUser.setId(3L);
rootUser.setName("root");
  
group.getUsers().add(guestUser);
group.getUsers().add(rootUser);
  
String jsonString = JSON.toJSONString(group);
  
System.out.println(jsonString);

output

{
      "name":"admin",
      "id":0,"users":[
                             {"name":"guest","id":2},
                             {"name":"root","id":3}
                         ]
}

Decode

Group group2 = JSON.parseObject(jsonString, Group.class);

 

 

下载地址:http://code.alibabatech.com/wiki/display/FastJSON/Overview

阅读全文
类别:Json 查看评论

posted on 2012-02-23 16:35 ProgramerNi 阅读(1122) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 
<2025年1月>
2930311234
567891011
12131415161718
19202122232425
2627282930311
2345678

导航

统计

常用链接

留言簿

随笔档案

文章分类

文章档案

搜索

最新评论