Java json转Map,转bean,转List

引用jackson

/**
*  json转Map,转bean,转List by http://blog.csdn.net/21aspnet/
*  需要jackjson jar包
*/
public class JsonUtil {

    /**
     * Object转Json
     */
    public static String ObjectToJson(Object value) {
        try {
            ObjectMapper mapper = new ObjectMapper();
            String js = mapper.writeValueAsString(value);
            return js;
        } catch (Exception e) {
            System.out.println(e.toString());
            return "Error";
        }
    }

    /**
     * Json转Object
     */
    public static Object JsonToList(String json, Class bean) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        JavaType javaType = mapper.getTypeFactory().constructParametricType(List.class, bean);
        return mapper.readValue(json, javaType);
    }

    /**
     * Json转Object
     */
    public static Object JsonToBean(String json, Class bean) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        JavaType javaType = mapper.getTypeFactory().uncheckedSimpleType(bean);
        return mapper.readValue(json, javaType);
    }

    /**
     * Json转Map
     */
    public static Map> JsonToMap(String json) {
        try {
            ObjectMapper mapper = new ObjectMapper();
            Map> maps = mapper.readValue(json, Map.class);                      
            return maps;
        } catch (Exception e) {
            System.out.println(e.toString());
            return null;
        }
    }
}


 

你可能感兴趣的:(Java)