JSON 简单封装

阅读更多
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

/**
 * 后台数据转换成JSON操作的一些方法
 * 
 * @author wrj
 * 
 */
public class JSONUtils {

	/**
	 * 类型form
	 */
	public static String _JOSN_TYPE_FORM_="form";
	
	/**
	 * 类型AJAX
	 */
	public static String _JOSN_TYPE_AJAX_="ajax";
	
	/**
	 * 类型AJAX
	 */
	public static String _JOSN_TYPE_STORE_="store";
	/**
	 * 成功success
	 */
	public static String _JOSN_SUCCESS_="success";
	
	/**
	 * 操作信息
	 */
	private static String _JSON_MSG_ = "msg";

	/**
	 * 
	 */
	private static JsonConfig config = new JsonConfig();

	static {
		config.registerJsonValueProcessor(java.sql.Date.class, new JsDateJsonValueProcessorEx("yyyy-MM-dd"));
		
		config.registerJsonValueProcessor(java.sql.Time.class, new JsDateJsonValueProcessorEx("HH:mm:ss"));

		config.registerJsonValueProcessor(java.sql.Timestamp.class, new JsDateJsonValueProcessorEx("yyyy-MM-dd HH:mm:ss"));
	}

	/**
	 * 将返回消息转换为JSON数据格式返回
	 * 
	 * @param flag
	 *            成功失败标识
	 * @param msg
	 *            成功失败信息
	 * @return JSON格式的操作信息
	 */
	public static String oprationMsgToJSON(boolean flag,String msg) {
		//SuyLog.debug(jo.toString());
		return "{success:"+flag+","+JSONUtils._JSON_MSG_+":'"+msg+"'}";
		
		//return "{" + "\"_ajax_op_flag\":\"" + flag + "\",\"_ajax_op_msg\":" + "\"" + msg + "\"}";
	}

	/**
	 * 将返回消息转换为JSON数据格式返回
	 * 
	 * @param object
	 *            转换的对象 必须是List类型或Map类型的对象
	 * @param flag
	 *            成功失败标识
	 * @param msg
	 *            成功失败信息
	 * @return JSON格式的操作信息
	 */
	public static String oprationMsgToJSON(Object object, boolean flag, String msg,String type) {
		if (object instanceof Collection) {
			Map map = new HashMap(0);
			map.put("data", object);
			//map.put(JSONUtils._JSON_MSG_, msg);
			JSONObject jo = JSONObject.fromObject(map, config);
			
			//SuyLog.debug(jo.toString());
			//return "{success:"+flag+","+jo+"}";
			return jo.toString();
		} else if (object instanceof Map) {
			Map map = new HashMap(0);
			map.put("data", object);
			
			JSONObject jo = JSONObject.fromObject(map, config);	
			jo.put(JSONUtils._JSON_MSG_, msg);
			
			
			jo.put(JSONUtils._JOSN_SUCCESS_, flag);
			
			
			return jo.toString();
			//SuyLog.debug(jo.toString());
		}/* else if (object instanceof Page) {
			Map map = new HashMap(0);
			map.put("data", ((Page)object).getList());
			map.put("total", ((Page)object).getTotal());			
			JSONObject jo = JSONObject.fromObject(map, config);
			//SuyLog.debug(jo.toString());			
			 
			return jo.toString();
		} */else {
			Map map = new HashMap(0);			
			map.put(JSONUtils._JSON_MSG_, msg);
			JSONObject jo = JSONObject.fromObject(map);	
			 
			return "{success:"+flag+","+jo+"}";
			//SuyLog.debug(jo.toString());
			
		}

	}
	
	
	public static String convertListToJSONStr(List list){
		
		if(list==null){
			return "{}";
		}
		
		JSONArray ja = JSONArray.fromObject(list, config);
		
		return  ja.toString();
	}
	

	/**
	 * @param strJSON :
	 *            JSON字符串
	 * @return List
	 */
	public static List convertJSONStrToList(String strJSON) {
		JSONArray ja = JSONArray.fromObject(strJSON);
		List lst = new ArrayList();
		Map myRow = new HashMap(0);
		for (int i = 0; i < ja.size(); i++) {
			JSONObject jo = (JSONObject) ja.get(i);
			Iterator names = jo.keySet().iterator();
			while (names.hasNext()) {
				String name = (String) names.next();
				myRow.put(name, jo.get(name).toString());
			}

			lst.add(new HashMap(myRow));
			myRow.clear();
		}
		return lst;
	}

	/**
	 * @param Map 
	 *            
	 * @return jsonStr:JSON字符串
	 
	
	public static String convertMapToJSON(Map map){
		//清空null值
		map=BeanUtilsEx.parameterNullToString(map);
		//map=BeanUtilsEx.parameterCleanNullOrEmpty(map);
		//转成小写
		map=BeanUtilsEx.convertMapKeyToLowerCase(map);
		JSONObject jo = JSONObject.fromObject(map, config);	
		return jo.toString();
	}*/
	/** *//**
     * 从json对象集合表达式中得到一个java对象列表
     * @param jsonString
     * @param pojoClass
     * @return
     */
    public static List getList4Json(String jsonString, Class pojoClass){
        
        JSONArray jsonArray = JSONArray.fromObject(jsonString);
        JSONObject jsonObject;
        Object pojoValue;
        
        List list = new ArrayList();
        for ( int i = 0 ; i 
  

 

你可能感兴趣的:(json,java)