import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.anxin.ssk.util.json.ListBean;
import com.anxin.ssk.util.json.MapBean;
import com.sdicons.json.mapper.JSONMapper;
import com.sdicons.json.mapper.MapperException;
import com.sdicons.json.model.JSONArray;
import com.sdicons.json.model.JSONValue;
import com.sdicons.json.parser.JSONParser;
/**
* JSON工具类
*
*/
public class JSonUtil {
/**
* JAVA对象转换成JSON字符串
*
* @param obj JAVA对象
* @return JSON字符串
* @throws MapperException
*/
public static String obj2Json(Object obj) throws MapperException {
return obj2Json(obj, false);
}
/**
* JAVA数组对象转换成JSON字符串
*
* @param list JAVA数组对象
* @return JSON字符串
* @throws MapperException
*/
public static String obj2Json(List> list) throws MapperException {
if (list == null || list.size() == 0) {
return "{}";
}
StringBuilder jsonString = new StringBuilder();
for (int i = 0; i < list.size(); i++) {
if (i != 0) {
jsonString.append(",");
}
Class> cla = list.get(i);
jsonString.append(obj2Json(cla, false));
}
return jsonString.toString();
}
/**
* JAVA集合对象转换成JSON字符串
*
* @param map JAVA集合对象
* @return JSON字符串
* @throws MapperException
*/
public static String obj2Json(Map> map) throws MapperException {
if (map == null || map.size() == 0) {
return "{}";
}
StringBuilder jsonString = new StringBuilder();
Set keySet = map.keySet();
boolean isFirst = true;
for (String key : keySet) {
if (isFirst) {
isFirst = false;
} else {
jsonString.append(",");
}
Class> cla = map.get(key);
jsonString.append(obj2Json(cla, false));
}
return jsonString.toString();
}
/**
* JAVA集合对象转换成JSON字符串
*
* @param map JAVA集合对象
* @return JSON字符串
* @throws MapperException
*/
@SuppressWarnings("unchecked")
public static String map2Json(Map,?> map) throws MapperException {
MapBean mapBean = new MapBean((Map)map);
return obj2Json(mapBean);
}
/**
* JAVA数组对象转换成JSON字符串
*
* @param list JAVA数组对象
* @return JSON字符串
* @throws MapperException
*/
public static String list2Json(List> list) throws MapperException {
ListBean listBean = new ListBean(list);
return obj2Json(listBean);
}
/**
* 重载objectToJsonStr方法
*
* @param obj 需要转换的JAVA对象
* @param format 是否格式化
* @return JSON字符串
* @throws MapperException
*/
public static String obj2Json(Object obj, boolean format) throws MapperException {
JSONValue jsonValue = JSONMapper.toJSON(obj);
String jsonStr = jsonValue.render(format);
return jsonStr;
}
/**
* JSON字符串转换成JAVA对象
*
* @param jsonStr JSON字符串
* @param cla JAVA对象
* @return 转换后的对象
*/
public static Object json2Obj(String jsonStr, Class> cla) throws Exception {
Object obj = null;
try {
JSONParser parser = new JSONParser(new StringReader(jsonStr));
JSONValue jsonValue = parser.nextValue();
if (jsonValue instanceof com.sdicons.json.model.JSONArray) {
List list = new ArrayList();
JSONArray jsonArray = (JSONArray) jsonValue;
for (int i = 0; i < jsonArray.size(); i++) {
JSONValue jsonObj = jsonArray.get(i);
Object javaObj = JSONMapper.toJava(jsonObj, cla);
list.add(javaObj);
}
obj = list;
} else if (jsonValue instanceof com.sdicons.json.model.JSONObject) {
obj = JSONMapper.toJava(jsonValue, cla);
} else {
obj = jsonValue;
}
} catch (Exception e) {
e.printStackTrace();
}
return obj;
}
}
分享到:
2012-08-15 17:17
浏览 22218
评论
5 楼
李崇XYZ
2016-10-20
4 楼
小小诺
2014-05-18
import com.anxin.ssk.util.json.ListBean;
import com.anxin.ssk.util.json.MapBean;
这两个类是博主自己写的吗?? 有没有相应的jar包?
3 楼
飞鱼的小雨
2014-03-17
import com.anxin.ssk.util.json.ListBean;
import com.anxin.ssk.util.json.MapBean;
import com.sdicons.json.mapper.JSONMapper;
import com.sdicons.json.mapper.MapperException;
import com.sdicons.json.model.JSONArray;
import com.sdicons.json.model.JSONValue;
import com.sdicons.json.parser.JSONParser;
这些都是json相关jar包里面的吗?
2 楼
飞鱼的小雨
2014-03-17
import com.sdicons.json.mapper.JSONMapper;
import com.sdicons.json.mapper.MapperException;
import com.sdicons.json.model.JSONArray;
import com.sdicons.json.model.JSONValue;
import com.sdicons.json.parser.JSONParser;
这几个都是jar包里的东西吗?
1 楼
happy250go
2012-08-27
写的不错~