日期:2014-05-16  浏览次数:20320 次

使用JSON-LIB转换JAVA对象

使用JSON-LIB可以极大的简化JAVA对象转换成JSON对象所需进行的操作,更可以避免人工操作生成JSON对象字符串时带来的麻烦和误操作:
使用JSON-LIB,首先要有几个支持的包:
http://json-lib.sourceforge.net 下载 json-lib-1.1-jdk15.jar
commons-lang.jar commons-logging.jar commons-beanutils.jar? 这些包可在tomcat/comon/lib下找到
EZMorph 下载地址http://ezmorph.sourceforge.net
morph-1.0.1 下载地址:http://morph.sourceforge.net

使用例子:
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;

public class JSONTest {
??? public static void main(String[] args) {??
??? ??? JSONTest j = new JSONTest();??
??????? j.ObjectList2json();??
??? }???
??
??? public void ObjectList2json(){
??? ??? Map map = new HashMap();
??? ???
??? ??? List jlist = new ArrayList();
??? ??? JSONTestBean bean1 = new JSONTestBean("zhangbo","123123");
??? ??? JSONTestBean bean2 = new JSONTestBean("lisi","65489");
??? ??? 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);??
??? }
???
??? public void arr2json() {??
??????? boolean[] boolArray = new boolean[] { true, false, true };??
??????? JSONArray jsonArray = JSONArray.fromObject(boolArray);??
??????? System.out.println(jsonArray);??
??????? // prints [true,false,true]??
??? }??
??
??? 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"]??
??? }??
??
??? public void createJson() {??
??????? JSONArray jsonArray = JSONArray.fromObject("['json','is','easy']");??
??????? System.out.println(jsonArray);??
??????? // prints ["json","is","easy"]??
??? }??
??
??? 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]; }]??
??? }??
??
??? 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]; }}?
???????? */??
??? }??
??
??? 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();??
??? }??
}

其它两个测试实体Bean:
public class JSONTestBean {

??? private String userName;

??? private String password;

??? public JSONTestBean() {

??? }

??? public JSONTestBean(String username, String passwor