使用JSON-LIB可以極大的簡(jiǎn)化JAVA對(duì)象轉(zhuǎn)換成JSON對(duì)象所需進(jìn)行的操作,更可以避免人工操作生成JSON對(duì)象字符串時(shí)帶來(lái)的麻煩和誤操作:
使用JSON-LIB,首先要有幾個(gè)支持的包:
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);
-
- }
-
- public void list2json() {
- List list = new ArrayList();
- list.add("first");
- list.add("second");
- JSONArray jsonArray = JSONArray.fromObject(list);
- System.out.println(jsonArray);
-
- }
-
- public void createJson() {
- JSONArray jsonArray = JSONArray.fromObject("['json','is','easy']");
- System.out.println(jsonArray);
-
- }
-
- 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);
-
-
-
- }
-
- public void bean2json() {
- JSONObject jsonObject = JSONObject.fromObject(new JSONTestBean("zhangbo","234234"));
- System.out.println(jsonObject);
-
-
-
-
-
-
- }
-
- public void json2bean() {
- String json = "{name=\"json2\",func1:true,pojoId:1,func2:function(a){ return a; },options:['1','2']}";
-
-
- System.out.println();
- }
- }
-
- 其它兩個(gè)測(cè)試實(shí)體Bean:
- 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;
- }
- }
-
-
- 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;
- }
-
- }
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(); } }其它兩個(gè)測(cè)試實(shí)體Bean: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; }}//===================================================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; }}
使用起來(lái)很方便,有了JSON-LIB的支持,可以使開(kāi)發(fā)者輕松構(gòu)建起基于JSON的AJAX應(yīng)用程序
附加:關(guān)于使用JSON-LIB轉(zhuǎn)換帶有DATE類型的對(duì)象需要額外的一些設(shè)置
- JsonConfig cfg=new JsonConfig();
- cfg.registerJsonValueProcessor(java.util.Date.class, new JsonValueProcessorImpl());
- cfg.registerJsonValueProcessor(java.sql.Date.class, new JsonValueProcessorImpl());
- JSONObject obj = JSONObject.fromObject(info,cfg);
JsonConfig cfg=new JsonConfig();cfg.registerJsonValueProcessor(java.util.Date.class, new JsonValueProcessorImpl());cfg.registerJsonValueProcessor(java.sql.Date.class, new JsonValueProcessorImpl());JSONObject obj = JSONObject.fromObject(info,cfg);
JsonValueProcessorImpl為實(shí)現(xiàn)了源代碼中的接口JsonValueProcessor
- import java.text.SimpleDateFormat;
- import java.util.Date;
-
- import net.sf.json.JsonConfig;
- import net.sf.json.processors.JsonValueProcessor;
-
- public class JsonValueProcessorImpl implements JsonValueProcessor{
- private String format="yyyy-MM-dd";
- public JsonValueProcessorImpl(){
-
- }
- public JsonValueProcessorImpl(String format){
- this.format=format;
- }
- public Object processArrayValue(Object value, JsonConfig jsonConfig) {
- String[] obj={};
- if(value instanceof Date[]){
- SimpleDateFormat sf=new SimpleDateFormat(format);
- Date[] dates=(Date[])value;
- obj =new String[dates.length];
- for (int i = 0; i < dates.length; i++) {
- obj[i]=sf.format(dates[i]);
- }
- }
- return obj;
- }
-
- public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
- if(value instanceof Date){
- String str=new SimpleDateFormat(format).format((Date)value);
- return str;
- }
- return value.toString();
- }
-
- public String getFormat() {
- return format;
- }
-
- public void setFormat(String format) {
- this.format = format;
- }
-
- }
import java.text.SimpleDateFormat;import java.util.Date;import net.sf.json.JsonConfig;import net.sf.json.processors.JsonValueProcessor;public class JsonValueProcessorImpl implements JsonValueProcessor{ private String format="yyyy-MM-dd"; public JsonValueProcessorImpl(){ } public JsonValueProcessorImpl(String format){ this.format=format; } public Object processArrayValue(Object value, JsonConfig jsonConfig) { String[] obj={}; if(value instanceof Date[]){ SimpleDateFormat sf=new SimpleDateFormat(format); Date[] dates=(Date[])value; obj =new String[dates.length]; for (int i = 0; i < dates.length; i++) { obj[i]=sf.format(dates[i]); } } return obj; } public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) { if(value instanceof Date){ String str=new SimpleDateFormat(format).format((Date)value); return str; } return value.toString(); } public String getFormat() { return format; } public void setFormat(String format) { this.format = format; } }
這也只是實(shí)現(xiàn)了將DATE類型轉(zhuǎn)換成yyyy-MM-dd的格式...測(cè)試一下吧!~~~