Json-lib的简单使用

前言

JSON-lib是一个能够将java bean/map/collection/java array/xml转换成JSON并且反过来将JSON转换成java对象的类库,它至少需要依赖如下的jar包,其他的依赖包可以参考官网http://json-lib.sourceforge.net/dependencies.html。不过现在并不推荐使用这个组件,官方也已经停止更新,可以使用jackson json。

jakarta commons-lang 2.5
jakarta commons-beanutils 1.8.0
jakarta commons-collections 3.2.1
jakarta commons-logging 1.1.1
ezmorph 1.0.6


JsonConfig参考官网的api文档说明http://json-lib.sourceforge.net/apidocs/jdk15/index.html

JSON是一个接口,其实现类有JSONArray, JSONNull, JSONObject



JSONSerializer

例子

1、基本用法,其中JSON转换后的java对象是MorphDynaBean
import java.util.Date;

import net.sf.ezmorph.bean.MorphDynaBean;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;

public class Demo {
	public static void main(String[] args) {
		System.out.println(JSONSerializer.toJSON(new Date()));// 打印当前日期的json格式
		MorphDynaBean db = (MorphDynaBean) JSONSerializer.toJava(JSONObject
				.fromObject("{\"Hello\":\"你好\"}"));// 返回一个动态bean
		System.out.println(db.get("Hello"));
	}
}
{"date":1,"day":3,"hours":10,"minutes":6,"month":6,"seconds":23,"time":1435716383919,"timezoneOffset":-480,"year":115}
你好


JSONObject 

例子

1、将bean/map转换为JSONObject,这里设置JsonConfig的一个属性,即循环的处理策略,其他属性的用法类似
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.CycleDetectionStrategy;

public class Demo {
	public static class CycleObject {
		private String id;
		private String name;
		private CycleObject co = this;

		public String getId() {
			return id;
		}

		public void setId(String id) {
			this.id = id;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public CycleObject getCo() {
			return co;
		}

		public void setCo(CycleObject co) {
			this.co = co;
		}
	}

	public static void main(String[] args) {
		CycleObject object = new CycleObject();
		object.setId("1");
		object.setName("co");
		JsonConfig jsonConfig = new JsonConfig();
		// LENIENT:循环的成员变量返回null;NOPROP:不显示循环的成员变量;STRICT:报异常
		jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
		JSONObject json = JSONObject.fromObject(object, jsonConfig);
		System.out.println(json);
	}
}
{"co":null,"id":"1","name":"co"}

2、JSON转换成MorphDynaBean
import net.sf.ezmorph.bean.MorphDynaBean;
import net.sf.json.JSONObject;

public class Demo {
	public static class CycleObject {
		private String id;
		private String name;
		private CycleObject co = this;

		public String getId() {
			return id;
		}

		public void setId(String id) {
			this.id = id;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public CycleObject getCo() {
			return co;
		}

		public void setCo(CycleObject co) {
			this.co = co;
		}
	}

	public static void main(String[] args) {
		String json = "{name=\"json\",bool:true,int:1}";
		JSONObject jsonObject = JSONObject.fromObject(json);
		MorphDynaBean bean = (MorphDynaBean) JSONObject.toBean(jsonObject);
		System.out.println(bean.get("name"));
	}
}
json

3、JSON转换成普通bean
import net.sf.json.JSONObject;

public class Demo {
	public static class Bean {
		private String id;
		private String name;

		public String getId() {
			return id;
		}

		public void setId(String id) {
			this.id = id;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		@Override
		public String toString() {
			return "id=" + id + ";name=" + name;
		}
	}

	public static void main(String[] args) {
		String json = "{id=\"1\",name=\"json\"}";
		JSONObject jsonObject = JSONObject.fromObject(json);
		Bean bean = (Bean) JSONObject.toBean(jsonObject, Bean.class);
		System.out.println(bean);
	}
}
id=1;name=json


JSONArray

例子

1、
    boolean[] boolArray = new boolean[]{true,false,true};  
    JSONArray jsonArray = JSONArray.fromObject( boolArray );  
    System.out.println( jsonArray );  
    // prints [true,false,true]  

    List list = new ArrayList();  
    list.add( "first" );  
    list.add( "second" );  
    JSONArray jsonArray = JSONArray.fromObject( list );  
    System.out.println( jsonArray );  
    // prints ["first","second"]  

    JSONArray jsonArray = JSONArray.fromObject( "['json','is','easy']" );  
    System.out.println( jsonArray );  
    // prints ["json","is","easy"]  

2、
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class Demo {
	public static class Bean {
		private String id;
		private String name;

		public String getId() {
			return id;
		}

		public void setId(String id) {
			this.id = id;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		@Override
		public String toString() {
			return "id=" + id + ";name=" + name;
		}
	}

	public static void main(String[] args) {
		boolean[] boolArray = new boolean[] { true, false, true };
		JSONArray jsonArray = JSONArray.fromObject(boolArray);
		Bean bean = new Bean();
		bean.setId("1");
		bean.setName("json");
		JSONObject jsonObject = JSONObject.fromObject(bean);
		jsonObject.put("data", jsonArray);
		System.out.println(jsonObject);
	}
}
{"id":"1","name":"json","data":[true,false,true]}

xml

这个自己参考官网的例子吧 http://json-lib.sourceforge.net/usage.html


你可能感兴趣的:(Json-lib的简单使用)