package cn.dip.util;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
public class JsonUtil {
/**
* 公用的json分页方法
* @param start 起始页
* @param limit 终止页
* @param list 传入的所有数据数组
* @param config 设置过滤字段
* @return
*/
public static String pageFromList(int start,int limit,List list,JsonConfig config){
int total = start + limit;
if(total>list.size()){
total = list.size();
}
List templist = list.subList(start, total);
Map<String, Object> map = new HashMap<String, Object>();
map.put("totalProperty", list.size());
map.put("topics", templist);
String logs = JSONObject.fromObject(map,config).toString();
return logs;
}
public static String pageFromListWithoutConfig(int start,int limit,List list){
int total = start + limit;
if(total>list.size()){
total = list.size();
}
List templist = list.subList(start, total);
Map<String, Object> map = new HashMap<String, Object>();
map.put("totalProperty", list.size());
map.put("topics", templist);
String logs = JSONObject.fromObject(map).toString();
return logs;
}
public static String jsonFromObject(Object object,JsonConfig config){
String jsonString = JSONObject.fromObject(object,config).toString();
return jsonString;
}
public static String jsonFromList(List list , JsonConfig config){
String jsonString = JSONArray.fromObject(list,config).toString();
return jsonString;
}
public static String jsonFromListWithoutConfig(List list ){
String jsonString = JSONArray.fromObject(list).toString();
return jsonString;
}
public static String jsonFromObjectWithoutConfig(Object object){
return JSONObject.fromObject(object).toString();
}
public static String javabean2json(Object object) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("success", true);
map.put("data", object);
return JSONObject.fromObject(map).toString();
}
public static String javabean2jsonwithconfig(Object object,JsonConfig config) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("success", true);
map.put("data", object);
return JSONObject.fromObject(map,config).toString();
}
}