详细解决方案
fastjson 根本使用方法
热度:897 发布时间:2012-11-01 11:11:31.0
fastjson 基本使用方法
?fastjson 用于生成和解析 JSON 数据的简单示例
[代码] [Java]代码
03 |
???? private String name;
|
05 |
???? public Long getId() { return id; }
|
06 |
???? public void setId(Long id) { this .id = id; }
|
08 |
???? public String getName() { return name; }
|
09 |
???? public void setName(String name) { this .name = name; }
|
[代码] [Java]代码
03 |
???? private String name;
|
04 |
???? private List<User> users = new ArrayList<User>();
|
06 |
???? public Long getId() { return id; }
|
07 |
???? public void setId(Long id) { this .id = id; }
|
09 |
???? public String getName() { return name; }
|
10 |
???? public void setName(String name) { this .name = name; }
|
12 |
???? public List<User> getUsers() { return users; }
|
13 |
???? public void setUsers(List<User> users) { this .users = users; }
|
[代码] 编码
01 |
import com.alibaba.fastjson.JSON;
|
03 |
Group group = new Group();
|
05 |
group.setName( "admin" );
|
07 |
User guestUser = new User();
|
09 |
guestUser.setName( "guest" );
|
11 |
User rootUser = new User();
|
13 |
rootUser.setName( "root" );
|
15 |
group.getUsers().add(guestUser); |
16 |
group.getUsers().add(rootUser); |
18 |
String jsonString = JSON.toJSONString(group); |
20 |
System.out.println(jsonString); |
[代码] 输出结果
4 |
????????????????????????????? {"name":"guest","id":2},
|
5 |
????????????????????????????? {"name":"root","id":3}
|
6 |
????????????????????????? ]
|
[代码] 解码
1 |
Group group2 = JSON.parseObject(jsonString, Group. class );
|