org.voovan.tools.json.JSONEncode Maven / Gradle / Ivy
Show all versions of voovan-common Show documentation
package org.voovan.tools.json;
import org.voovan.tools.TObject;
import org.voovan.tools.TString;
import org.voovan.tools.reflect.TReflect;
import java.util.Collection;
import java.util.List;
import java.util.Map;
/**
* JSON打包类
*
* @author helyho
*
* Voovan Framework.
* WebSite: https://github.com/helyho/Voovan
* Licence: Apache v2 License
*/
public class JSONEncode {
/**
* 分析自定义对象为JSON字符串
*
* @param object 自定义对象
* @return JSON字符串
* @throws ReflectiveOperationException
*/
private static String complexObject(Object object) throws ReflectiveOperationException {
return mapObject(TReflect.getMapfromObject(object));
}
/**
* 分析Map对象为JSON字符串
*
* @param mapObject map对象
* @return JSON字符串
* @throws ReflectiveOperationException
*/
private static String mapObject(Map, ?> mapObject) throws ReflectiveOperationException {
String mapString = "{";
StringBuilder ContentStringBuilder = new StringBuilder();
String ContentString = null;
Object[] keys = mapObject.keySet().toArray();
for (Object mapkey : keys) {
Object key = fromObject(mapkey);
String Value = fromObject(mapObject.get(mapkey));
ContentStringBuilder.append(key);
ContentStringBuilder.append(":");
ContentStringBuilder.append(Value);
ContentStringBuilder.append(",");
}
ContentString = ContentStringBuilder.toString();
if (!ContentString.trim().isEmpty()){
ContentString = ContentString.substring(0, ContentString.length() - 1);
}
mapString = mapString + ContentString + "}";
return mapString;
}
/**
* 分析Collection对象为JSON字符串
*
* @param listObject List对象
* @return JSON字符串
* @throws ReflectiveOperationException
*/
private static String CollectionObject(List