日期:2014-05-16 浏览次数:20354 次
package com.hoo.entity; public class Student { private int id; private String name; private String email; private String address; private Birthday birthday; //setter、getter public String toString() { return this.name + "#" + this.id + "#" + this.address + "#" + this.birthday + "#" + this.email; } } Birthday.java package com.hoo.entity; public class Birthday { private String birthday; public Birthday(String birthday) { super(); this.birthday = birthday; } //setter、getter public Birthday() {} @Override public String toString() { return this.birthday; } }
package com.hoo.test; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.Collection; import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import net.sf.json.JSON; import net.sf.json.JSONArray; import net.sf.json.JSONFunction; import net.sf.json.JSONObject; import net.sf.json.JSONSerializer; import net.sf.json.JsonConfig; import net.sf.json.processors.JsonValueProcessor; import net.sf.json.util.PropertyFilter; import net.sf.json.xml.XMLSerializer; import org.apache.commons.beanutils.PropertyUtils; import org.junit.After; import org.junit.Before; import org.junit.Test; import com.hoo.entity.Birthday; import com.hoo.entity.Student; /** * <b>function:</b> 用json-lib转换java对象到JSON字符串 * 读取json字符串到java对象,序列化jsonObject到xml * json-lib-version: json-lib-2.3-jdk15.jar * 依赖包: * commons-beanutils.jar * commons-collections-3.2.jar * ezmorph-1.0.3.jar * commons-lang.jar * commons-logging.jar * @author hoojo * @createDate Nov 28, 2010 2:28:39 PM * @file JsonlibTest.java * @package com.hoo.test * @project WebHttpUtils * @blog http://blog.csdn.net/IBM_hoojo * @email hoojo_@126.com * @version 1.0 */ @SuppressWarnings({ "deprecation", "unchecked" }) public class JsonlibTest { private JSONArray jsonArray = null; private JSONObject jsonObject = null; private Student bean = null; @Before public void init() { jsonArray = new JSONArray(); jsonObject = new JSONObject(); bean = new Student(); bean.setAddress("address"); bean.setEmail("email"); bean.setId(1); bean.setName("haha"); Birthday day = new Birthday(); day.setBirthday("2010-11-22"); bean.setBirthday(day); } @After public void destory() { jsonArray = null; jsonObject = null; bean = null; System.gc(); } public final void fail(String string) { System.out.println(string); } public final void failRed(String string) { System.err.println(string); } }