【实践】java bean与json的序列化与反序列化【serialize】

com.fasterxml.jackson.core
jackson-databind
2.10.0

1、通过该项目下的某个类,可以将对象和 json格式的字符串互转。

public static String toJson(Object object){
    try {
        return objectMapper.writeValueAsString(object);
    }
    catch(Exception ex){
        throw new IllegalStateException("can not write json:" + object, ex);
    }
}
Open Declaration String com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(Object value) throws JsonProcessingException


Method that can be used to serialize any Java value as a String. Functionally equivalent to calling writeValue(Writer, Object) with java.io.StringWriter and constructing String, but more efficient. 

Note: prior to version 2.1, throws clause included IOException; 2.1 removed it.

Parameters:
value 
Throws:
JsonProcessingException
发布了345 篇原创文章 · 获赞 2 · 访问量 2106

猜你喜欢

转载自blog.csdn.net/m0_37681589/article/details/103688135