- packagecomm;
- importjava.text.SimpleDateFormat;
- importjava.util.ArrayList;
- importjava.util.Collection;
- importjava.util.Date;
- importjava.util.HashMap;
- importjava.util.Iterator;
- importjava.util.List;
- importjava.util.Map;
- importnet.sf.ezmorph.MorpherRegistry;
- importnet.sf.ezmorph.object.DateMorpher;
- importnet.sf.json.JSONArray;
- importnet.sf.json.JSONObject;
- importnet.sf.json.JsonConfig;
- importnet.sf.json.processors.JsonValueProcessor;
- importnet.sf.json.util.JSONUtils;
- importnet.sf.json.xml.XMLSerializer;
- publicclassJsonUtil{
- static{
- MorpherRegistrymr=JSONUtils.getMorpherRegistry();
- DateMorpherdm=newDateMorpher(newString[]{Util.YYYY_MM_DD,
- Util.YYYY_MM_DD_HH_MM_ss,Util.HH_MM_ss,Util.YYYYMMDD,
- Util.YYYYMMDDHHMMSS,Util.HHMMss});
- mr.registerMorpher(dm);
- }
- publicstaticObjectgetDtoFromJsonObjStr(StringjsonObjStr,Classclazz){
- returnJSONObject.toBean(JSONObject.fromObject(jsonObjStr),clazz);
- }
- publicstaticObjectgetDtoFromJsonObjStr(StringjsonObjStr,Classclazz,MapclassMap){
- returnJSONObject.toBean(JSONObject.fromObject(jsonObjStr),clazz,classMap);
- }
- publicstaticObject[]getArrFromJsonArrStr(StringjsonArrStr){
- returnJSONArray.fromObject(jsonArrStr).toArray();
- }
- publicstaticObject[]getDtoArrFromJsonArrStr(StringjsonArrStr,Classclazz){
- JSONArrayjsonArr=JSONArray.fromObject(jsonArrStr);
- Object[]objArr=newObject[jsonArr.size()];
- for(inti=0;i<jsonArr.size();i++){
- objArr[i]=JSONObject.toBean(jsonArr.getJSONObject(i),clazz);
- }
- returnobjArr;
- }
- publicstaticObject[]getDtoArrFromJsonArrStr(StringjsonArrStr,Classclazz,
- MapclassMap){
- JSONArrayarray=JSONArray.fromObject(jsonArrStr);
- Object[]obj=newObject[array.size()];
- for(inti=0;i<array.size();i++){
- JSONObjectjsonObject=array.getJSONObject(i);
- obj[i]=JSONObject.toBean(jsonObject,clazz,classMap);
- }
- returnobj;
- }
- publicstaticListgetListFromJsonArrStr(StringjsonArrStr){
- JSONArrayjsonArr=JSONArray.fromObject(jsonArrStr);
- Listlist=newArrayList();
- for(inti=0;i<jsonArr.size();i++){
- list.add(jsonArr.get(i));
- }
- returnlist;
- }
- publicstaticListgetListFromJsonArrStr(StringjsonArrStr,Classclazz){
- JSONArrayjsonArr=JSONArray.fromObject(jsonArrStr);
- Listlist=newArrayList();
- for(inti=0;i<jsonArr.size();i++){
- list.add(JSONObject.toBean(jsonArr.getJSONObject(i),clazz));
- }
- returnlist;
- }
- publicstaticListgetListFromJsonArrStr(StringjsonArrStr,Classclazz,MapclassMap){
- JSONArrayjsonArr=JSONArray.fromObject(jsonArrStr);
- Listlist=newArrayList();
- for(inti=0;i<jsonArr.size();i++){
- list.add(JSONObject.toBean(jsonArr.getJSONObject(i),clazz,classMap));
- }
- returnlist;
- }
- publicstaticMapgetMapFromJsonObjStr(StringjsonObjStr){
- JSONObjectjsonObject=JSONObject.fromObject(jsonObjStr);
- Mapmap=newHashMap();
- for(Iteratoriter=jsonObject.keys();iter.hasNext();){
- Stringkey=(String)iter.next();
- map.put(key,jsonObject.get(key));
- }
- returnmap;
- }
- publicstaticMapgetMapFromJsonObjStr(StringjsonObjStr,Classclazz){
- JSONObjectjsonObject=JSONObject.fromObject(jsonObjStr);
- Mapmap=newHashMap();
- for(Iteratoriter=jsonObject.keys();iter.hasNext();){
- Stringkey=(String)iter.next();
- map.put(key,JSONObject.toBean(jsonObject.getJSONObject(key),clazz));
- }
- returnmap;
- }
- publicstaticMapgetMapFromJsonObjStr(StringjsonObjStr,Classclazz,MapclassMap){
- JSONObjectjsonObject=JSONObject.fromObject(jsonObjStr);
- Mapmap=newHashMap();
- for(Iteratoriter=jsonObject.keys();iter.hasNext();){
- Stringkey=(String)iter.next();
- map.put(key,JSONObject
- .toBean(jsonObject.getJSONObject(key),clazz,classMap));
- }
- returnmap;
- }
- publicstaticStringgetJsonStr(Objectobj){
- StringjsonStr=null;
- JsonConfigjsonCfg=newJsonConfig();
- jsonCfg.registerJsonValueProcessor(java.util.Date.class,
- newJsonDateValueProcessor(Util.YYYY_MM_DD_HH_MM_ss));
- if(obj==null){
- return"{}";
- }
- if(objinstanceofCollection||objinstanceofObject[]){
- jsonStr=JSONArray.fromObject(obj,jsonCfg).toString();
- }else{
- jsonStr=JSONObject.fromObject(obj,jsonCfg).toString();
- }
- returnjsonStr;
- }
- publicstaticStringgetXMLFromObj(Objectobj){
- XMLSerializerxmlSerial=newXMLSerializer();
- JsonConfigjsonCfg=newJsonConfig();
- jsonCfg.registerJsonValueProcessor(java.util.Date.class,
- newJsonDateValueProcessor(Util.YYYY_MM_DD_HH_MM_ss));
- if((String.class.isInstance(obj)&&String.valueOf(obj).startsWith("["))
- ||obj.getClass().isArray()||Collection.class.isInstance(obj)){
- JSONArrayjsonArr=JSONArray.fromObject(obj,jsonCfg);
- returnxmlSerial.write(jsonArr);
- }else{
- JSONObjectjsonObj=JSONObject.fromObject(obj,jsonCfg);
- returnxmlSerial.write(jsonObj);
- }
- }
- publicstaticStringgetJsonStrFromXML(Stringxml){
- XMLSerializerxmlSerial=newXMLSerializer();
- returnString.valueOf(xmlSerial.read(xml));
- }
- }
- classJsonDateValueProcessorimplementsJsonValueProcessor{
- privateStringformat=Util.YYYY_MM_DD_HH_MM_ss;
- publicJsonDateValueProcessor(){
- }
- publicJsonDateValueProcessor(Stringformat){
- this.format=format;
- }
- publicObjectprocessArrayValue(Objectvalue,JsonConfigjsonConfig){
- returnprocess(value,jsonConfig);
- }
- publicObjectprocessObjectValue(Stringkey,Objectvalue,JsonConfigjsonConfig){
- returnprocess(value,jsonConfig);
- }
- privateObjectprocess(Objectvalue,JsonConfigjsonConfig){
- if(valueinstanceofDate){
- Stringstr=newSimpleDateFormat(format).format((Date)value);
- returnstr;
- }
- returnvalue==null?null:value.toString();
- }
- publicStringgetFormat(){
- returnformat;
- }
- publicvoidsetFormat(Stringformat){
- this.format=format;
- }
- }
package comm;
import java.text.SimpleDateFormat;
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 net.sf.ezmorph.MorpherRegistry;
import net.sf.ezmorph.object.DateMorpher;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;
import net.sf.json.util.JSONUtils;
import net.sf.json.xml.XMLSerializer;
public class JsonUtil {
/**
* 设置日期转换格式
*/
static {
//注册器
MorpherRegistry mr = JSONUtils.getMorpherRegistry();
//可转换的日期格式,即Json串中可以出现以下格式的日期与时间
DateMorpher dm = new DateMorpher(new String[] { Util.YYYY_MM_DD,
Util.YYYY_MM_DD_HH_MM_ss, Util.HH_MM_ss, Util.YYYYMMDD,
Util.YYYYMMDDHHMMSS, Util.HHMMss });
mr.registerMorpher(dm);
}
/**
* 从json串转换成实体对象
* @param jsonObjStr e.g. {'name':'get','dateAttr':'2009-11-12'}
* @param clazz Person.class
* @return
*/
public static Object getDtoFromJsonObjStr(String jsonObjStr, Class clazz) {
return JSONObject.toBean(JSONObject.fromObject(jsonObjStr), clazz);
}
/**
* 从json串转换成实体对象,并且实体集合属性存有另外实体Bean
* @param jsonObjStr e.g. {'data':[{'name':'get'},{'name':'set'}]}
* @param clazz e.g. MyBean.class
* @param classMap e.g. classMap.put("data", Person.class)
* @return Object
*/
public static Object getDtoFromJsonObjStr(String jsonObjStr, Class clazz, Map classMap) {
return JSONObject.toBean(JSONObject.fromObject(jsonObjStr), clazz, classMap);
}
/**
* 把一个json数组串转换成普通数组
* @param jsonArrStr e.g. ['get',1,true,null]
* @return Object[]
*/
public static Object[] getArrFromJsonArrStr(String jsonArrStr) {
return JSONArray.fromObject(jsonArrStr).toArray();
}
/**
* 把一个json数组串转换成实体数组
* @param jsonArrStr e.g. [{'name':'get'},{'name':'set'}]
* @param clazz e.g. Person.class
* @return Object[]
*/
public static Object[] getDtoArrFromJsonArrStr(String jsonArrStr, Class clazz) {
JSONArray jsonArr = JSONArray.fromObject(jsonArrStr);
Object[] objArr = new Object[jsonArr.size()];
for (int i = 0; i < jsonArr.size(); i++) {
objArr[i] = JSONObject.toBean(jsonArr.getJSONObject(i), clazz);
}
return objArr;
}
/**
* 把一个json数组串转换成实体数组,且数组元素的属性含有另外实例Bean
* @param jsonArrStr e.g. [{'data':[{'name':'get'}]},{'data':[{'name':'set'}]}]
* @param clazz e.g. MyBean.class
* @param classMap e.g. classMap.put("data", Person.class)
* @return Object[]
*/
public static Object[] getDtoArrFromJsonArrStr(String jsonArrStr, Class clazz,
Map classMap) {
JSONArray array = JSONArray.fromObject(jsonArrStr);
Object[] obj = new Object[array.size()];
for (int i = 0; i < array.size(); i++) {
JSONObject jsonObject = array.getJSONObject(i);
obj[i] = JSONObject.toBean(jsonObject, clazz, classMap);
}
return obj;
}
/**
* 把一个json数组串转换成存放普通类型元素的集合
* @param jsonArrStr e.g. ['get',1,true,null]
* @return List
*/
public static List getListFromJsonArrStr(String jsonArrStr) {
JSONArray jsonArr = JSONArray.fromObject(jsonArrStr);
List list = new ArrayList();
for (int i = 0; i < jsonArr.size(); i++) {
list.add(jsonArr.get(i));
}
return list;
}
/**
* 把一个json数组串转换成集合,且集合里存放的为实例Bean
* @param jsonArrStr e.g. [{'name':'get'},{'name':'set'}]
* @param clazz
* @return List
*/
public static List getListFromJsonArrStr(String jsonArrStr, Class clazz) {
JSONArray jsonArr = JSONArray.fromObject(jsonArrStr);
List list = new ArrayList();
for (int i = 0; i < jsonArr.size(); i++) {
list.add(JSONObject.toBean(jsonArr.getJSONObject(i), clazz));
}
return list;
}
/**
* 把一个json数组串转换成集合,且集合里的对象的属性含有另外实例Bean
* @param jsonArrStr e.g. [{'data':[{'name':'get'}]},{'data':[{'name':'set'}]}]
* @param clazz e.g. MyBean.class
* @param classMap e.g. classMap.put("data", Person.class)
* @return List
*/
public static List getListFromJsonArrStr(String jsonArrStr, Class clazz, Map classMap) {
JSONArray jsonArr = JSONArray.fromObject(jsonArrStr);
List list = new ArrayList();
for (int i = 0; i < jsonArr.size(); i++) {
list.add(JSONObject.toBean(jsonArr.getJSONObject(i), clazz, classMap));
}
return list;
}
/**
* 把json对象串转换成map对象
* @param jsonObjStr e.g. {'name':'get','int':1,'double',1.1,'null':null}
* @return Map
*/
public static Map getMapFromJsonObjStr(String jsonObjStr) {
JSONObject jsonObject = JSONObject.fromObject(jsonObjStr);
Map map = new HashMap();
for (Iterator iter = jsonObject.keys(); iter.hasNext();) {
String key = (String) iter.next();
map.put(key, jsonObject.get(key));
}
return map;
}
/**
* 把json对象串转换成map对象,且map对象里存放的为其他实体Bean
* @param jsonObjStr e.g. {'data1':{'name':'get'},'data2':{'name':'set'}}
* @param clazz e.g. Person.class
* @return Map
*/
public static Map getMapFromJsonObjStr(String jsonObjStr, Class clazz) {
JSONObject jsonObject = JSONObject.fromObject(jsonObjStr);
Map map = new HashMap();
for (Iterator iter = jsonObject.keys(); iter.hasNext();) {
String key = (String) iter.next();
map.put(key, JSONObject.toBean(jsonObject.getJSONObject(key), clazz));
}
return map;
}
/**
* 把json对象串转换成map对象,且map对象里存放的其他实体Bean还含有另外实体Bean
* @param jsonObjStr e.g. {'mybean':{'data':[{'name':'get'}]}}
* @param clazz e.g. MyBean.class
* @param classMap e.g. classMap.put("data", Person.class)
* @return Map
*/
public static Map getMapFromJsonObjStr(String jsonObjStr, Class clazz, Map classMap) {
JSONObject jsonObject = JSONObject.fromObject(jsonObjStr);
Map map = new HashMap();
for (Iterator iter = jsonObject.keys(); iter.hasNext();) {
String key = (String) iter.next();
map.put(key, JSONObject
.toBean(jsonObject.getJSONObject(key), clazz, classMap));
}
return map;
}
/**
* 把实体Bean、Map对象、数组、列表集合转换成Json串
* @param obj
* @return
* @throws Exception String
*/
public static String getJsonStr(Object obj) {
String jsonStr = null;
//Json配置
JsonConfig jsonCfg = new JsonConfig();
//注册日期处理器
jsonCfg.registerJsonValueProcessor(java.util.Date.class,
new JsonDateValueProcessor(Util.YYYY_MM_DD_HH_MM_ss));
if (obj == null) {
return "{}";
}
if (obj instanceof Collection || obj instanceof Object[]) {
jsonStr = JSONArray.fromObject(obj, jsonCfg).toString();
} else {
jsonStr = JSONObject.fromObject(obj, jsonCfg).toString();
}
return jsonStr;
}
/**
* 把json串、数组、集合(collection map)、实体Bean转换成XML
* XMLSerializer API:
* http://json-lib.sourceforge.net/apidocs/net/sf/json/xml/XMLSerializer.html
* 具体实例请参考:
* http://json-lib.sourceforge.net/xref-test/net/sf/json/xml/TestXMLSerializer_writes.html
* http://json-lib.sourceforge.net/xref-test/net/sf/json/xml/TestXMLSerializer_writes.html
* @param obj
* @return
* @throws Exception String
*/
public static String getXMLFromObj(Object obj) {
XMLSerializer xmlSerial = new XMLSerializer();
//Json配置
JsonConfig jsonCfg = new JsonConfig();
//注册日期处理器
jsonCfg.registerJsonValueProcessor(java.util.Date.class,
new JsonDateValueProcessor(Util.YYYY_MM_DD_HH_MM_ss));
if ((String.class.isInstance(obj) && String.valueOf(obj).startsWith("["))
|| obj.getClass().isArray() || Collection.class.isInstance(obj)) {
JSONArray jsonArr = JSONArray.fromObject(obj, jsonCfg);
return xmlSerial.write(jsonArr);
} else {
JSONObject jsonObj = JSONObject.fromObject(obj, jsonCfg);
return xmlSerial.write(jsonObj);
}
}
/**
* 从XML转json串
* @param xml
* @return String
*/
public static String getJsonStrFromXML(String xml) {
XMLSerializer xmlSerial = new XMLSerializer();
return String.valueOf(xmlSerial.read(xml));
}
}
/**
* json日期值处理器实现
* (C) 2009-9-11, jzj
*/
class JsonDateValueProcessor implements JsonValueProcessor {
private String format = Util.YYYY_MM_DD_HH_MM_ss;
public JsonDateValueProcessor() {
}
public JsonDateValueProcessor(String format) {
this.format = format;
}
public Object processArrayValue(Object value, JsonConfig jsonConfig) {
return process(value, jsonConfig);
}
public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
return process(value, jsonConfig);
}
private Object process(Object value, JsonConfig jsonConfig) {
if (value instanceof Date) {
String str = new SimpleDateFormat(format).format((Date) value);
return str;
}
return value == null ? null : value.toString();
}
public String getFormat() {
return format;
}
public void setFormat(String format) {
this.format = format;
}
}
- packagecomm.test;
- importjava.text.SimpleDateFormat;
- importjava.util.ArrayList;
- importjava.util.Date;
- importjava.util.HashMap;
- importjava.util.Iterator;
- importjava.util.LinkedHashMap;
- importjava.util.LinkedHashSet;
- importjava.util.List;
- importjava.util.Map;
- importjava.util.Set;
- importjunit.framework.TestCase;
- importcomm.JsonUtil;
- publicclassJsonUtilTestextendsTestCase{
- publicvoidtestGetDtoFromJsonObjStr1(){
- Stringjson="{'name':'get','dateAttr':'2009-11-12'}";
- Personps=(Person)JsonUtil.getDtoFromJsonObjStr(json,Person.class);
- System.out.println(ps.getName());
- SimpleDateFormatsdf=newSimpleDateFormat("yyyy-MM-dd");
- System.out.println(sdf.format(ps.getDateAttr()));
- }
- publicvoidtestGetDtoFromJsonObjStr3(){
- Stringjson="{'data':[{'name':'get'},{'name':'set'}]}";
- MapclassMap=newHashMap();
- classMap.put("data",Person.class);
- MyBeanmyBean=(MyBean)JsonUtil.getDtoFromJsonObjStr(json,MyBean.class,
- classMap);
- System.out.println(myBean.getData().get(0).getClass()+"name="
- +((Person)myBean.getData().get(0)).getName());
- System.out.println(myBean.getData().get(1).getClass()+"name="
- +((Person)myBean.getData().get(1)).getName());
- }
- publicvoidtestGetArrFromJsonArrStr(){
- Stringjson="['get',1,true,null]";
- Object[]objArr=JsonUtil.getArrFromJsonArrStr(json);
- for(inti=0;i<objArr.length;i++){
- System.out.println(objArr[i].getClass()+""+objArr[i]);
- }
- }
- publicvoidtestGetDtoArrFromJsonArrStr1(){
- Stringjson="[{'name':'get'},{'name':'set'}]";
- Object[]objArr=JsonUtil.getDtoArrFromJsonArrStr(json,Person.class);
- for(inti=0;i<objArr.length;i++){
- System.out.println(objArr[i].getClass()+"name="
- +((Person)objArr[i]).getName());
- }
- }
- publicvoidtestGetDtoArrFromJsonArrStr2(){
- Stringjson="[{'data':[{'name':'get'}]},{'data':[{'name':'set'}]}]";
- MapclassMap=newHashMap();
- classMap.put("data",Person.class);
- Object[]objArr=JsonUtil.getDtoArrFromJsonArrStr(json,MyBean.class,classMap);
- for(inti=0;i<objArr.length;i++){
- System.out.println(((MyBean)objArr[i]).getData().get(0).getClass()
- +"name="
- +((Person)((MyBean)objArr[i]).getData().get(0)).getName());
- }
- }
- publicvoidtestGetListFromJsonArrStr1(){
- Stringjson="['get',1,true,null]";
- Listlist=JsonUtil.getListFromJsonArrStr(json);
- for(inti=0;i<list.size();i++){
- System.out.println(list.get(i).getClass()+""+list.get(i));
- }
- }
- publicvoidtestGetListFromJsonArrStr2(){
- Stringjson="[{'name':'get'},{'name':'set'}]";
- Listlist=JsonUtil.getListFromJsonArrStr(json,Person.class);
- for(inti=0;i<list.size();i++){
- System.out.println(list.get(i).getClass()+"name="
- +((Person)list.get(i)).getName());
- }
- }
- publicvoidtestGetListFromJsonArrStr3(){
- Stringjson="[{'data':[{'name':'get'}]},{'data':[{'name':'set'}]}]";
- MapclassMap=newHashMap();
- classMap.put("data",Person.class);
- Listlist=JsonUtil.getListFromJsonArrStr(json,MyBean.class,classMap);
- for(inti=0;i<list.size();i++){
- System.out.println(((MyBean)list.get(i)).getData().get(0).getClass()
- +"name="
- +((Person)((MyBean)list.get(i)).getData().get(0)).getName());
- }
- }
- publicvoidtestGetMapFromJsonObjStr(){
- Stringjson="{'name':'get','int':1,'double':1.1,'null':null}";
- Mapmap=JsonUtil.getMapFromJsonObjStr(json);
- for(Iteratoriter=map.keySet().iterator();iter.hasNext();){
- System.out.println(map.get(iter.next()).getClass());
- }
- }
- publicvoidtestGetMapFromJsonObjStr2(){
- Stringjson="{'data1':{'name':'get'},'data2':{'name':'set'}}";
- Mapmap=JsonUtil.getMapFromJsonObjStr(json,Person.class);
- for(Iteratoriter=map.keySet().iterator();iter.hasNext();){
- Stringkey=(String)iter.next();
- System.out.println(map.get(key).getClass()+"name="
- +((Person)map.get(key)).getName());
- }
- }
- publicvoidtestGetMapFromJsonObjStr3(){
- Stringjson="{'mybean':{'data':[{'name':'get'}]}}";
- MapclassMap=newHashMap();
- classMap.put("data",Person.class);
- Mapmap=JsonUtil.getMapFromJsonObjStr(json,MyBean.class,classMap);
- for(Iteratoriter=map.keySet().iterator();iter.hasNext();){
- Stringkey=(String)iter.next();
- Objecto=((MyBean)map.get(key)).getData().get(0);
- System.out.println(o.getClass()+"name="+((Person)o).getName());
- }
- }
- publicvoidtestgetJsonStr1(){
- Personps=newPerson();
- ps.setDateAttr(newDate());
- ps.setName("get");
- MyBeanmyBean=newMyBean();
- Listlist=newArrayList();
- list.add(ps);
- myBean.setData(list);
- System.out.println(JsonUtil.getJsonStr(myBean));
- }
- publicvoidtestgetJsonStr2(){
- Personps=newPerson();
- ps.setDateAttr(newDate());
- ps.setName("get");
- Mapmap=newLinkedHashMap();
- map.put("person1",ps);
- System.out.println(JsonUtil.getJsonStr(map));
- }
- publicvoidtestgetJsonStr3(){
- Personps=newPerson();
- ps.setDateAttr(newDate());
- ps.setName("get");
- Person[]personArr=newPerson[1];
- personArr[0]=ps;
- System.out.println(JsonUtil.getJsonStr(personArr));
- }
- publicvoidtestgetJsonStr4(){
- Personps=newPerson();
- ps.setDateAttr(newDate());
- ps.setName("get");
- Listlist=newArrayList();
- list.add(ps);
- System.out.println(JsonUtil.getJsonStr(list));
- }
- publicvoidtestgetJsonStr5(){
- Personps=newPerson();
- ps.setDateAttr(newDate());
- ps.setName("get");
- Setset=newLinkedHashSet();
- set.add(ps);
- System.out.println(JsonUtil.getJsonStr(set));
- }
- publicvoidtestGetXMLFromObj1(){
- System.out.println(JsonUtil
- .getXMLFromObj("{/"name/":/"json/",/"bool/":true,/"int/":1}"));
- }
- publicvoidtestGetXMLFromObj2(){
- System.out.println(JsonUtil.getXMLFromObj("[1,2,3]"));
- }
- publicvoidtestGetXMLFromObj3(){
- Personps=newPerson();
- ps.setDateAttr(newDate());
- ps.setName("get");
- Person[]personArr=newPerson[2];
- personArr[0]=ps;
- System.out.println(JsonUtil.getXMLFromObj(personArr));
- }
- publicvoidtestGetXMLFromObj4(){
- Personps=newPerson();
- ps.setDateAttr(newDate());
- ps.setName("get");
- Listlist=newArrayList();
- list.add(ps);
- System.out.println(JsonUtil.getXMLFromObj(list));
- }
- publicvoidtestGetXMLFromObj5(){
- Personps=newPerson();
- ps.setDateAttr(newDate());
- ps.setName("get");
- Setset=newLinkedHashSet();
- set.add(ps);
- System.out.println(JsonUtil.getXMLFromObj(set));
- }
- publicvoidtestGetXMLFromObj6(){
- Personps=newPerson();
- ps.setDateAttr(newDate());
- ps.setName("get");
- Mapmap=newHashMap();
- map.put("person1",ps);
- System.out.println(JsonUtil.getXMLFromObj(map));
- }
- publicvoidtestGetXMLFromObj7(){
- Personps=newPerson();
- ps.setDateAttr(newDate());
- ps.setName("get");
- System.out.println(JsonUtil.getXMLFromObj(ps));
- }
- publicvoidtestGetJsonStrFromXML1(){
- Stringxml="<o><dateAttrtype='string'>2009-09-1207:13:02</dateAttr>"
- +"<nametype='string'>get</name></o>";
- System.out.println(JsonUtil.getJsonStrFromXML(xml));
- }
- publicvoidtestGetJsonStrFromXML2(){
- Stringxml="<aclass='array'><eclass='object'><dateAttrtype='string'>2009-09-1207:04:38</dateAttr>"
- +"<nametype='string'>get</name></e></a>";
- System.out.println(JsonUtil.getJsonStrFromXML(xml));
- }
- }