这几天在学习前后台数据交换,由于项目的需要。自学了json字符串的相关操作。为了方便以后的学习,现在将学习的简单的demo贴在下面:
1、要用json-lib还需要以下几个jar包:
commons-beanutils.jar
commons-collections.jar
commons-lang-2.4.jar
commons-logging.jar
ezmorph-1.0.4.jar
json-lib-2.3-jdk15.jar
官方网址:http://json-lib.sourceforge.net/
2、贴出相关代码:
2-1、User.java类
package cn.kpchen.model;
public class User {
private int id;
private String name;
private String email;
private String phone;
private String qq;
private Depart depart;
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 String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getQq() {
return qq;
}
public void setQq(String qq) {
this.qq = qq;
}
public Depart getDepart() {
return depart;
}
public void setDepart(Depart depart) {
this.depart = depart;
}
}
2-2、Depart.java源码
package cn.kpchen.model;
public class Depart {
private int id;
private String departNo;
private String departName;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDepartNo() {
return departNo;
}
public void setDepartNo(String departNo) {
this.departNo = departNo;
}
public String getDepartName() {
return departName;
}
public void setDepartName(String departName) {
this.departName = departName;
}
}
2-3、JsonTest.java 源码:
package cn.kpchen.test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import cn.kpchen.model.Depart;
import cn.kpchen.model.User;
/**
* 依赖包: commons-beanutils.jar commons-collections-3.2.jar ezmorph-1.0.3.jar commons-lang.jar commons-logging.jar
*/
public class JsonTest {
/**
* @param args
*/
public static void main(String[] args) {
objToJSON();
listOrSetToJSON();
mapToJSON();
}
/**
* 将map集合转化为json字符串
*/
private static void mapToJSON() {
Map<String, Object> map = new HashMap<String, Object>();
User user = new User();
user.setId(1);
user.setName("小明");
user.setEmail("xiaoming@163.com");
user.setPhone("18356985496");
user.setQq("123546");
Depart depart = new Depart();
depart.setId(1);
depart.setDepartNo("111");
depart.setDepartName("财务部");
user.setDepart(depart);
map.put("totle", "200");
map.put("user", user);
JSONObject jsonObjectFromMap = JSONObject.fromObject(map);
System.out.println(jsonObjectFromMap);
}
/**
* 将list或set集合转化成json字符串
*/
private static void listOrSetToJSON() {
List<User> lists = new ArrayList<User>();
for (int i = 0; i < 5; i++) {
User user = new User();
user.setId(1 + i);
user.setName("小明" + i);
user.setEmail("xiaoming@163.com" + i);
user.setPhone("18356985496" + i);
user.setQq("123546" + i);
Depart depart = new Depart();
depart.setId(1);
depart.setDepartNo("111" + i);
depart.setDepartName("财务部" + i);
user.setDepart(depart);
lists.add(user);
}
JSONArray jsonArrayFromList = JSONArray.fromObject(lists);
System.out.println(jsonArrayFromList.toString());
}
/**
* 将对象转化为json字符串
*/
public static void objToJSON() {
User user = new User();
user.setId(1);
user.setName("小明");
user.setEmail("xiaoming@163.com");
user.setPhone("18356985496");
user.setQq("123546");
Depart depart = new Depart();
depart.setId(1);
depart.setDepartNo("111");
depart.setDepartName("财务部");
user.setDepart(depart);
JSONObject jsonObject = JSONObject.fromObject(user);
System.out.println(jsonObject.toString());
}
}
源码:http://download.csdn.net/detail/kpchen_0508/8119751
源码:http://pan.baidu.com/s/1GVtue