现在的位置: 首页 > 综合 > 正文

Json工具类

2018年02月12日 ⁄ 综合 ⁄ 共 7480字 ⁄ 字号 评论关闭

package cn.fiyo.base.util;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.web.bind.ServletRequestDataBinder;

import cn.easecom.platform.module.basic.web.user.UserContext;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class JsonUtils {
	private static final Log log = LogFactory.getLog(JsonUtils.class);
	
	public JsonUtils() {
		
	}
	
	/**
	 * 向客户端输出JSON
	 */
	public static void outputJson(HttpServletResponse response, JSONObject json)
			throws Exception {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.write(json.toString());
		out.flush();
	}

	/** 
	 * 从一个JSON 对象字符格式中得到一个java对象
	 * 
	 */

	public static Object toBean(String jsonString, Class beanClass) {
		JSONObject jsonObject = JSONObject.fromObject(jsonString);
		Object object = JSONObject.toBean(jsonObject, beanClass);
		return object;
	}
	
	public static Object toBean(String jsonString, Class beanClass,Map map) {
		JSONObject jsonObject = JSONObject.fromObject(jsonString);
		Object object = JSONObject.toBean(jsonObject, beanClass,map);
		return object;
	}

	/** 
	 * 从json对象集合表达式中得到一个java对象列表
	 * @param jsonString:数组字符串[{key1:value1,key2:value2},{bean2},{bean3},...]
	 * @param pojoClass
	 * @return
	 */
	public static List toList(String jsonString, Class beanClass) {
		JSONArray jsonArray = JSONArray.fromObject(jsonString);
		List list = new ArrayList();
		for (int i = 0; i < jsonArray.size(); i++) {
			JSONObject jsonObject = jsonArray.getJSONObject(i);
			Object object = JSONObject.toBean(jsonObject, beanClass);
			list.add(object);
		}
		return list;
	}
	public static List toList(String jsonString, Class beanClass,Map map) {
		JSONArray jsonArray = JSONArray.fromObject(jsonString);
		List list = new ArrayList();
		for (int i = 0; i < jsonArray.size(); i++) {
			JSONObject jsonObject = jsonArray.getJSONObject(i);
			Object object = JSONObject.toBean(jsonObject, beanClass,map);
			list.add(object);
		}
		return list;
	}

	/** 
	 * 从json字符串中获取一个map,该map支持嵌套功能
	 * @param jsonString
	 * @return
	 */
	public static Map toMap(String jsonString) {
		JSONObject jsonObject = JSONObject.fromObject(jsonString);
		Iterator it = jsonObject.keys();
		Map map = new HashMap();
		while (it.hasNext()) {
			String key = (String) it.next();
			Object value = jsonObject.get(key);
			map.put(key, value);
		}
		return map;
	}
	
	/**
	 * 从json数组中得到相应java数组
	 * @param jsonString
	 * @return
	 */
	public static Object[] toObjectArray(String jsonString) {
		JSONArray jsonArray = JSONArray.fromObject(jsonString);
		return jsonArray.toArray();
	}

	/** 
	 * 从json解析出java字符串数组
	 * @param jsonString
	 * @return
	 */
	public static String[] toStringArray(String jsonString) {
		JSONArray jsonArray = JSONArray.fromObject(jsonString);
		String[] stringArray = new String[jsonArray.size()];
		for (int i = 0; i < jsonArray.size(); i++) {
			stringArray[i] = jsonArray.getString(i);
		}
		return stringArray;
	}

	/** 
	 * 从json解析出javaLong型对象数组
	 * @param jsonString
	 * @return
	 */
	public static Long[] toLongArray(String jsonString) {

		JSONArray jsonArray = JSONArray.fromObject(jsonString);
		Long[] longArray = new Long[jsonArray.size()];
		for (int i = 0; i < jsonArray.size(); i++) {
			longArray[i] = jsonArray.getLong(i);
		}
		return longArray;
	}

	/**
	 * 从json解析出java Integer型对象数组
	 * @param jsonString:[1,2,3,4]
	 * @return
	 */
	public static Integer[] toIntegerArray(String jsonString) {
		JSONArray jsonArray = JSONArray.fromObject(jsonString);
		Integer[] integerArray = new Integer[jsonArray.size()];
		for (int i = 0; i < jsonArray.size(); i++) {
			integerArray[i] = jsonArray.getInt(i);
		}
		return integerArray;
	}

	
	/**
	 * 从json中解析出java Double型对象数组
	 * @param jsonString
	 * @return
	 */
	public static Double[] toDoubleArray(String jsonString) {
		JSONArray jsonArray = JSONArray.fromObject(jsonString);
		Double[] doubleArray = new Double[jsonArray.size()];
		for (int i = 0; i < jsonArray.size(); i++) {
			doubleArray[i] = jsonArray.getDouble(i);
		}
		return doubleArray;
	}

	/**
	 * 将java对象转换成json字符串
	 * @param javaObj
	 * @return
	 */
	public static String toJsonString(Object object) {
		JSONObject json = JSONObject.fromObject(object);
		return json.toString();
	}

	/*
	 * 将java对象转化为json数组字符串
	 * [{"name":"name1","id":"id1"},{"name":"name2","id":"id2"}]
	 */
	public static String toJsonArrayString(List<Object> list) {
		JSONArray jsonArray = JSONArray.fromObject(list);
		return jsonArray.toString();
	}

	private String dateFormat = "yyyy-MM-dd";

    public void setDateFormat(String dateFormat){
        this.dateFormat = dateFormat;
    }

    /**
     * 格式化日期字符串
     */
    public void initBinder(HttpServletRequest request,ServletRequestDataBinder binder){
        SimpleDateFormat dateFormat = new SimpleDateFormat(this.dateFormat);
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
        binder.registerCustomEditor(String.class,new StringTrimmerEditor(false));
    }
   
    @SuppressWarnings("unchecked")
    public static void outputJsonResponse(HttpServletResponse response,boolean result){
    	JSONObject json = new JSONObject();
        json.put("result", result);
        String content=json.toString();
        flushResponse(response,content);
    }
    /*
     * {"result":true,"msg":"abc"}
     */
    @SuppressWarnings("unchecked")
    public static void outputJsonResponse(HttpServletResponse response,boolean result,String message){
    	JSONObject json = new JSONObject();
        json.put("result", result);
        json.put("message", message);
        String content=json.toString();
        flushResponse(response,content);
    }
    @SuppressWarnings("unchecked")
    public static void outputJsonResponse(HttpServletResponse response,boolean result,String message,String userData){
    	JSONObject json = new JSONObject();
        json.put("result", result);
        json.put("message", message);
        json.put("userData", userData);
        String content=json.toString();
        flushResponse(response,content);
    }
    /*
     * {"result":true,"data":{"key1":"value1","key2":"value2"}}
     */
    @SuppressWarnings("unchecked")
    public static void outputJsonResponse(HttpServletResponse response,boolean result,String message,Map userData){
    	JSONObject json = new JSONObject();
        json.put("result", result);
        json.put("message", message);
        if(userData!=null && userData.size()>0)
            json.put("userData", userData);
        String content=json.toString();
        flushResponse(response,content);
    }
    
    /*
     * 输出json数据
     * 输出格式为:{result:true,data:[{"name":"name1","id":"id1"},{"name":"name2","id":"id2"}]}
     */
    public static void outputJsonResponse(HttpServletResponse response,boolean result,List list) {
    	JSONObject json = new JSONObject();
    	json.put("result", result);
        if (list!=null && list.size()>0){
            JSONArray jsonArray = JSONArray.fromObject(list);
            json.put("data", jsonArray);
        }
        String content=json.toString();
        flushResponse(response,content);
    }
       
    /*
     * 输出ext列表的json数据
     * 输出格式为:{totalProperty:12,root:[{"name":"name1","id":"id1"},{"name":"name2","id":"id2"}]}
     */
    public static void outputJsonDataForExt(HttpServletResponse response,int totalProperty,List list) {
    	JSONObject json = new JSONObject();
        json.put("totalProperty", totalProperty);
        if (list!=null && list.size()>0){
            JSONArray jsonArray = JSONArray.fromObject(list);
            json.put("root", jsonArray);
        }
        String content=json.toString();
        flushResponse(response,content);
    }
    /**
     * Method to flush a String as response.
     * @param response
     * @param responseContent
     * @throws IOException
     */
    public static void flushResponse(HttpServletResponse response,String responseContent){
	    response.setCharacterEncoding("UTF-8");
		try {
			PrintWriter writer = response.getWriter();
			writer.write(responseContent);
	        writer.flush();
	        writer.close();
		} catch (IOException e) {
			log.error(e.getMessage());
		}
    }
    /**
     * @param response
     */
    public static void outputXML(HttpServletResponse response, String xml) throws Exception {
        response.setContentType("text/xml; charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.write(xml);
        out.flush();
    }
    
    /*
     * 属性拷贝
     */
    public void copyProperties(Object dest, Object orig) {
		try {
			BeanUtils.copyProperties(dest, orig);
		} catch (Exception ex) {
			log.error("Copy property error: " + ex.toString());
		}
	}
}

转自:http://sdfiyon.iteye.com/blog/1197519

抱歉!评论已关闭.