编码前还需要导入的jar包: commons-beanutils-1.7.0.jar 、 commons-lang-2.5.jar 、commons-logging-1.0.4.jar 、 ezmorph-1.0.6.jar 、 json-lib-1.1-jdk15.jar、morph-1.1.1.jar
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 com.ch.entity.JSONTestBean;
import com.ch.entity.Props;
/**
* Java对象转换为Json对象
* @author CHH
* @since 2012-08-04
*/
public class TestJson {
public static void main(String[] args) {
TestJson j = new TestJson();
j.ObjectList2json();
}
/**
* List对象转换为Json
*/
public void ObjectList2json() {
Map map = new HashMap();
List jlist = new ArrayList();
//JSONTestBean类的构造方法
JSONTestBean bean1 = new JSONTestBean("zhangbo", "123123");
JSONTestBean bean2 = new JSONTestBean("lisi", "65489");
//Props类的构造方法
Props props = new Props("127.0.0.1", "8008");
jlist.add(bean1);
jlist.add(bean2);
map.put("Props", props);
map.put("jsonObjectList", jlist);
JSONArray jsonArray = JSONArray.fromObject(map);
System.out.println(jsonArray);
}
/**
* 布尔数组转换为Json
*/
public void arr2json() {
boolean[] boolArray = new boolean[] { true, false, true };
JSONArray jsonArray = JSONArray.fromObject(boolArray);
System.out.println(jsonArray);
// prints [true,false,true]
}
/**
* List转换为Json
*/
public void list2json() {
List list = new ArrayList();
list.add("first");
list.add("second");
JSONArray jsonArray = JSONArray.fromObject(list);
System.out.println(jsonArray);
// prints ["first","second"]
}
/**
* 创建Json格式的数据
*/
public void createJson() {
JSONArray jsonArray = JSONArray.fromObject("['json','is','easy']");
System.out.println(jsonArray);
// prints ["json","is","easy"]
}
/**
* Map转换为Json
*/
public void map2json() {
Map map = new HashMap();
map.put("name", "json");
map.put("bool", Boolean.TRUE);
map.put("int", new Integer(1));
map.put("arr", new String[] { "a", "b" });
map.put("func", "function(i){ return this.arr[i]; }");
JSONObject json = JSONObject.fromObject(map);
System.out.println(json);
// prints
// ["name":"json","bool":true,"int":1,"arr":["a","b"],"func":function(i){
// return this.arr[i]; }]
}
/**
* 类对象转换为Json
*/
public void bean2json() {
JSONObject jsonObject = JSONObject.fromObject(new JSONTestBean(
"zhangbo", "234234"));
System.out.println(jsonObject);
/*
* prints {"func1":function(i){ return this.options[i];
* },"pojoId":1,"name":"json","func2":function(i){ return
* this.options[i]; }}
*/
}
/**
* Json转换为类对象
*/
public void json2bean() {
String json = "{name=\"json2\",func1:true,pojoId:1,func2:function(a){ return a; },options:['1','2']}";
// JSONObject jb = JSONObject.fromString(json);
// JSONObject.toBean(jb, MyBean.class);
System.out.println(json);
}
}
<pre class="html" name="code">View JSONTestBean
/**
* 实体类一
*
* @author CHH
* @since 2012-08-04
*/
public class JSONTestBean {
private String userName;
private String password;
public JSONTestBean() {
}
public JSONTestBean(String username, String password) {
this.userName = username;
this.password = password;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
View Props
/**
*
* @author CHH
* @since 2012-08-04
*/
public class Props {
private String ip;
private String port;
public Props() {
}
public Props(String ip, String port) {
this.ip = ip;
this.port = port;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getPort() {
return port;
}
public void setPort(String port) {
this.port = port;
}
}