json工具类

来源:互联网 发布:抽奖软件制作 编辑:程序博客网 时间:2024/06/04 18:44

/**
* @author {Lelonta} json的工具类
*/
public class JsonUtils {

// 定义jackson对象private static final ObjectMapper MAPPER = new ObjectMapper();/** * 将对象转换成json字符串。 * <p> * Title: pojoToJson * </p> * <p> * Description: * </p> *  * @param data * @return * @throws IOException */public static String objectToJson(Object data) throws IOException {    try {        String string = MAPPER.writeValueAsString(data);        return string;    } catch (JsonProcessingException e) {        e.printStackTrace();    }    return null;}/** * 将json结果集转化为对象 *  * @param jsonData *            json数据 * @param clazz *            对象中的object类型 * @return */public static <T> T jsonToPojo(String jsonData, Class<T> beanType) {    try {        T t = MAPPER.readValue(jsonData, beanType);        return t;    } catch (Exception e) {        e.printStackTrace();    }    return null;}/** * 将json数据转换成pojo对象list * <p> * Title: jsonToList * </p> * <p> * Description: * </p> *  * @param jsonData * @param beanType * @return */public static <T> List<T> jsonToList(String jsonData, Class<T> beanType) {    JavaType javaType = MAPPER.getTypeFactory().constructParametricType(            List.class, beanType);    try {        List<T> list = MAPPER.readValue(jsonData, javaType);        return list;    } catch (Exception e) {        e.printStackTrace();    }    return null;}/** *  * jsonObjectToPo: (将json转换成自定义po类).<br/> *  * @author: Lelonta * @param json * @param beanType * @return */@SuppressWarnings({ "unchecked", "hiding" })public static <T> T jsonObjectToPo(String json, Class<T> beanType) {    JSONObject jsonObject = JSONObject.fromObject(json);    T t = (T) JSONObject.toBean(jsonObject, beanType);    return t;}/** *  * strToHashMap: (字符串转换成map).<br/> * @author: Lelonta * @param jsonStr * @return */public static HashMap<String, Object> strToHashMap(String jsonStr) {    HashMap<String, Object> map = new HashMap<String, Object>();    // 最外层解析    JSONObject json = JSONObject.fromObject(jsonStr);    for (Object k : json.keySet()) {        Object v = json.get(k);        // 如果内层还是数组的话,继续解析        if (v instanceof JSONArray) {            List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();            Iterator<JSONObject> it = ((JSONArray) v).iterator();            while (it.hasNext()) {                JSONObject json2 = it.next();                list.add(strToHashMap(json2.toString()));            }            map.put(k.toString(), list);        } else {            map.put(k.toString(), v);        }    }    return map;}public static void main(String[] args) {    // String jsonMapStr = "{\"province\":\"上海\",\"sex\":\"男\"}";    String jsonString = "{\"PRDLIST\": [" + "{\"INSORGCODE\": \"1032\","            + "\"SALECODE\": \"10000086\"}," + "{\"PREM\": 7000,"            + "\"MONEYCODE\": \"0\"," + "\"RISK_STATUS\": \"0\"}],"            + "\"POLICYCODE\": \"2016139485682088\"}";    // HashMap<String, Object> map = new HashMap<String, Object>();    // map = strToHashMap(jsonString);    //    // System.out.println(map);    Map<String, Object> retMap = strToHashMap(jsonString);    System.out.println("retMap" + retMap);}

}

原创粉丝点击