1. 程式人生 > >Java常見幫助類(6)JSON幫助類

Java常見幫助類(6)JSON幫助類

大家常用的一個幫組類:

/**
 * Json工具類
 * 
 * @author 
 * 
 */
public class JsonUtil
{
    private static Logger logger = Logger.getLogger(JsonUtil.class);
    
    /**
    * 用fastjson 將json字串解析為一個 JavaBean
    * 
    * @param jsonString
    * @param cls
    * @return
    */
    public static <T> T parseBean(String jsonString, Class<T> cls)
    {
        T t = null;
        try
        {
            t = JSON.parseObject(jsonString, cls);
        }
        catch (Exception e)
        {
            // TODO: handle exception
            e.printStackTrace();
            logger.error(e.getMessage());
        }
        return t;
    }
    
    /**
    * 用fastjson 將json字串 解析成為一個 List<JavaBean> 及 List<String>
    * 
    * @param jsonString
    * @param cls
    * @return
    */
    public static <T> List<T> parseBeanArray(String jsonString, Class<T> cls)
    {
        List<T> list = new ArrayList<T>();
        try
        {
            list = JSON.parseArray(jsonString, cls);
        }
        catch (Exception e)
        {
            // TODO: handle exception
            e.printStackTrace();
        }
        return list;
    }
    
    /**
    * 用fastjson 將jsonString 解析成 List<Map<String,Object>>
    * 
    * @param jsonString
    * @return
    */
    public static List<Map<String, Object>> parseListMap(String jsonString)
    {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        try
        {
            // 兩種寫法
            // list = JSON.parseObject(jsonString, new
            // TypeReference<List<Map<String, Object>>>(){}.getType());
            
            list = JSON.parseObject(jsonString, new TypeReference<List<Map<String, Object>>>()
            {
            });
        }
        catch (Exception e)
        {
            // TODO: handle exception
            e.printStackTrace();
        }
        return list;
    }
    
    /**
    * 用fastjson 將Object 轉化為jsonString
    * 
    * @param object
    * @return
    */
    public static String toJsonString(Object object)
    {
        String jsonString = "";
        try
        {
            jsonString = JSON.toJSONString(object);
        }
        catch (Exception e)
        {
            // TODO: handle exception
            e.printStackTrace();
            logger.error(e.getMessage());
        }
        return jsonString;
    }
    
    public static void main(String[] args)
    {
        
        String strResult = "[{\"result_message\":\"返回掃描結果\",\"taskResultId\":608328,\"result_vulMiddle\":0,\"result_endTime\":\"\",\"result_returnCode\":0,\"result_vulSum\":0,\"domain\":\"gaokao.haedu.gov.cn\",\"result_vulLow\":0,\"info_message\":\"成功建立任務\",\"result_vulHigh\":0,\"info_returnCode\":0,\"result_startTime\":\"2016-07-11 11:28:03\"}]";
        List<Map<String, Object>> resultlist = new ArrayList<Map<String, Object>>();
        resultlist = JsonUtil.parseListMap(strResult);
        System.out.println(resultlist);
        System.out.println(resultlist.get(0).get("taskResultId"));
    }
}