- json是一个数据表示协议,或数据格式,如同xml一样。协议参看:http://www.json.org/。
- 基本所有的语言都支持json,java有很多实现,请参看:http://www.json.org。我这里用的是json-lib(http://json-lib.sourceforge.net/),并不是org.json。
- 创建测试代码
User.java,类容如下:
import java.io.Serializable;
public class User implements Serializable{
private static final long serialVersionUID = -8604848876542493025L;
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
CpxUser.java类内容:
import java.io.Serializable;
import java.util.Date;
public class CpxUser implements Serializable{
private User user;
private int age;
private String nickname;
private Date birDay;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public Date getBirDay() {
return birDay;
}
public void setBirDay(Date birDay) {
this.birDay = birDay;
}
}
Test.java类内容:
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("1.简单对象->json");
User u = new User();
u.setUsername("zzz");
u.setPassword("hhhh");
JSONObject jo = JSONObject.fromObject(u);
System.out.println(jo.toString());
System.out.println("2.map->json (结果和1一样)");
Map
mu.put("username", "zzz");
mu.put("password", "hhhh");
JSONObject jo1 = JSONObject.fromObject(mu);
System.out.println(jo1.toString());
System.out.println("3.复杂对象->json");
CpxUser cpxUser = new CpxUser();
cpxUser.setUser(u);
cpxUser.setAge(26);
cpxUser.setNickname("a chu");
cpxUser.setBirDay(new Date());
JSONObject jo2 = JSONObject.fromObject(cpxUser);
System.out.println(jo2.toString());
System.out.println("4.json->复杂对象 (可以debug看对象内容)");
String json_str = jo2.toString();
CpxUser cpxUser1 = (CpxUser)JSONObject.toBean(jo2, CpxUser.class);
System.out.println(cpxUser1.getAge());
System.out.println("5.简单对象数组->json");
User u1 = new User();
u1.setUsername("zzz1");
u1.setPassword("hhhh1");
User u2 = new User();
u2.setUsername("zzz2");
u2.setPassword("hhhh2");
User[] users = new User[]{u1,u2};
JSONArray arrs = JSONArray.fromObject(users);
System.out.println(arrs.toString());
}
}
- 输出结果
1.简单对象->json
{"username":"zzz","password":"hhhh"}
2.map->json (结果和1一样)
{"username":"zzz","password":"hhhh"}
3.复杂对象->json
{"nickname":"a chu","age":26,"birDay":{"time":1230096693386,"minutes":31,"seconds":33,"hours":13,"month":11,"year":108,"timezoneOffset":-480,"day":3,"date":24},"user":{"username":"zzz","password":"hhhh"}}
4.json->复杂对象 (可以debug看对象内容)
26
5.简单对象数组->json
[{"username":"zzz1","password":"hhhh1"},{"username":"zzz2","password":"hhhh2"}]
可以看出Java处理json非常简单,转换自如,相对xml描述精简很多,尤其在网络传输具有非常大的优势。java json包也非常轻量级,尤其是org.json(很遗憾该例没有使用该包)共有15个类,没有依赖,可以把源码放入工程。
注:不太喜欢org.json的实现,json-lib更面向对象,符合程序员的编码习惯(从上边的例子可以看出),缺点是依赖其它包。
没有评论:
发表评论