Map<String,List> map=creditTranInfoBusiness.getCountByTime();
//map to array for josn
Map<String,CreditTranInfoWebBean[]> drawDataMap=new HashMap<String,CreditTranInfoWebBean[]>();
for(String str:map.keySet()){
CreditTranInfoWebBean[] arr=(CreditTranInfoWebBean[])map.get(str).toArray(new CreditTranInfoWebBean[0]);
drawDataMap.put(str, arr);
}
logger.info("******************"+drawDataMap);
String result ="";
try {
logger.info("***************before**************");
result = JsonUtil.map2Json(drawDataMap);
如果声明drawDataMap时不写Map<String,CreditTranInfoWebBean[]> drawDataMap运行时抛出 throw new RuntimeException("Unsupported type: " + o.getClass().getName());这个的异常,但写了的话,报错
注:map这传值是没问题的。里面的值类似[{"key":"2","value":[{"id":0,"number":0,"timeOver":1289232300109,"timeStart":1289232000109,"tranCode":"2","tranCodeName":"信用卡"},{"id":0,"number":0,"timeOver":1289232600109,"timeStart":1289232300109,"tranCode":"2","tranCodeName":"信用卡"},{"id":0,"number":0,"timeOver":1289283000109,"timeStart":1289282700109,"tranCode":"2","tranCodeName":"信用卡"}]},{"key":"1","value":[]}]}
JsonUtil.Java
package com.yucheng.ecmp.ecmp4credit.web;
import java.util.Map;
import java.util.Set;
/**
* Util for converting Java object to JSON string.
*
* @author Xuefeng
*/
public final class JsonUtil {
/**
* Convert a Java object to JSON string.
*/
@SuppressWarnings("unchecked")
public static String toJson(Object o) {
if (o==null)
return "null";
if (o instanceof String)
return string2Json((String)o);
if (o instanceof Boolean)
return boolean2Json((Boolean)o);
if (o instanceof Number)
return number2Json((Number)o);
if (o instanceof Map)
return map2Json((Map<String, Object>)o);
if (o instanceof Object[])
return array2Json((Object[])o);
if (o instanceof int[])
return intArray2Json((int[])o);
if (o instanceof boolean[])
return booleanArray2Json((boolean[])o);
if (o instanceof long[])
return longArray2Json((long[])o);
if (o instanceof float[])
return floatArray2Json((float[])o);
if (o instanceof double[])
return doubleArray2Json((double[])o);
if (o instanceof short[])
return shortArray2Json((short[])o);
if (o instanceof byte[])
return byteArray2Json((byte[])o);
throw new RuntimeException("Unsupported type: " + o.getClass().getName());
}
static String array2Json(Object[] array) {
if (array.length==0)
return "[]";
StringBuilder sb = new StringBuilder(array.length << 4);
sb.append('[');
for (Object o : array) {
sb.append(toJson(o));
sb.append(',');
}
// set last ',' to ']':
sb.setCharAt(sb.length()-1, ']');
return sb.toString();