Json工具类

4 篇文章 0 订阅

JsonUtil 工具类:

代码如下:

package com.ceshi.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.lang.reflect.Field;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.CycleDetectionStrategy;
import org.apache.struts2.ServletActionContext;
import com.hxzk.cj.common.util.JsonDateValueProcessor;

/**
 * fastjson
 * 处理json返回信息、json转换
 * @author luvJie-7c

 *
 */
public class JsonUtil {


	/**
	 * 
	 * @param status  状态码
	 * @param message 返回信息
	 * @param token	 秘钥	
	 * @param obj	待转JSON的对象
	 * @return
	 * @throws Exception
	 */
	public static String getResponseDataJson(int status,String message, String token, Object obj){
		//拼接json串
		StringBuffer bf = new StringBuffer("{\"status\":"+status+"");
			bf.append(",\"message\":\""+message+"\"");
		if(token != null){
			bf.append(",\"token\":\""+token+"\"");
		}		
		if (obj != null) {
			bf.append(",\"result\":");
			bf.append(json);
		}
		bf.append("}");
		return bf.toString();
	}
	
	
	/**
	 * request 获取JSON数据
	 * @return 
	 * @throws Exception
	 */
	public static String requestGetJson(HttpServletRequest request) {
		//request获取body里的json数据
		BufferedReader streamReader = new BufferedReader( new InputStreamReader(request.getInputStream(), "UTF-8"));
		StringBuilder responseStrBuilder = new StringBuilder();
			String inputStr;
			while ((inputStr = streamReader.readLine()) != null){
			     responseStrBuilder.append(inputStr);
			 }
			 return responseStrBuilder.toString();
	}
	
	
	/**
	 * 写出响应JSON
	 * @param json 
	 * @return 
	 */
	public static void writeResponseJson(String json) {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");		
		PrintWriter out;
		try {
			out = response.getWriter();
			out.print(json);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
    /**
     * 对象转Map 反射处理
     * @param object 
     * @return
     */
    public static Map beanToMap(Object object){
        Map<String, Object> map = new HashMap<String, Object>();
        Field[] fields = object.getClass().getDeclaredFields();
            try {
                for (Field field : fields) {
                    field.setAccessible(true);
                    map.put(field.getName(), field.get(object));
                }
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        return map;
    }
    
    
    /**
     * 对象转Map 反射处理    去掉空值字段
     * @param object 
     * @return
     */
    public static Map beanValueToMap(Object object){
        Map<String, Object> map = new HashMap<String, Object>();
        Field[] fields = object.getClass().getDeclaredFields();
            try {
                for (Field field : fields) {
                    field.setAccessible(true);
                    if(field.get(object)!=null && !"".equals(field.get(object))){
                        map.put(field.getName(), field.get(object));
                    }
                }
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        return map;
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

luvJie-7c

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值