- ObjectMapper类,是jackson包下一个类,java针对处理对象与json之间关系转换用的
- 不推荐使用。使用其他构造来代替; 注意,可以设置序列化工厂
- 类中引入对象
-
private static final JavaType JSON_NODE_TYPE = TypeFactory.type(JsonNode.class); protected static final ClassIntrospector<? extends BeanDescription> DEFAULT_INTROSPECTOR; protected static final AnnotationIntrospector DEFAULT_ANNOTATION_INTROSPECTOR; protected static final VisibilityChecker<?> STD_VISIBILITY_CHECKER; protected final JsonFactory _jsonFactory; protected TypeResolverBuilder<?> _defaultTyper; protected VisibilityChecker<?> _visibilityChecker; protected SubtypeResolver _subtypeResolver; protected ClassLoader _valueClassLoader; protected SerializationConfig _serializationConfig; protected SerializerProvider _serializerProvider; protected SerializerFactory _serializerFactory; protected DeserializationConfig _deserializationConfig; protected DeserializerProvider _deserializerProvider; protected final ConcurrentHashMap<JavaType, JsonDeserializer<Object>> _rootDeserializers;
- 类中构造方法
-
public ObjectMapper() { this((JsonFactory)null, (SerializerProvider)null, (DeserializerProvider)null); } public ObjectMapper(JsonFactory jf) { this(jf, (SerializerProvider)null, (DeserializerProvider)null); } /** @deprecated */ @Deprecated public ObjectMapper(SerializerFactory sf) { this((JsonFactory)null, (SerializerProvider)null, (DeserializerProvider)null); this.setSerializerFactory(sf); } public ObjectMapper(JsonFactory jf, SerializerProvider sp, DeserializerProvider dp) { this(jf, sp, dp, (SerializationConfig)null, (DeserializationConfig)null); } public ObjectMapper(JsonFactory jf, SerializerProvider sp, DeserializerProvider dp, SerializationConfig sconfig, DeserializationConfig dconfig) { this._rootDeserializers = new ConcurrentHashMap(64, 0.6F, 2); this._jsonFactory = (JsonFactory)(jf == null ? new MappingJsonFactory(this) : jf); this._visibilityChecker = STD_VISIBILITY_CHECKER; this._serializationConfig = sconfig != null ? sconfig : new SerializationConfig(DEFAULT_INTROSPECTOR, DEFAULT_ANNOTATION_INTROSPECTOR, this._visibilityChecker, (SubtypeResolver)null); this._deserializationConfig = dconfig != null ? dconfig : new DeserializationConfig(DEFAULT_INTROSPECTOR, DEFAULT_ANNOTATION_INTROSPECTOR, this._visibilityChecker, (SubtypeResolver)null); this._serializerProvider = (SerializerProvider)(sp == null ? new StdSerializerProvider() : sp); this._deserializerProvider = (DeserializerProvider)(dp == null ? new StdDeserializerProvider() : dp); this._serializerFactory = BeanSerializerFactory.instance; }
- 需要引入jar包
-
import java.io.IOException; import org.codehaus.jackson.JsonParseException; import org.codehaus.jackson.map.JsonMappingException; import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.map.SerializationConfig;
经常会用到两个方法-
private static ObjectMapper objectMapper = new ObjectMapper();
-
String str = objectMapper.writeValueAsString(Object);//obj对象,返回json
-
Object obj = objectMapper.readValue(String, clazz.class);json,类对象,返回类对象
-
public static void main(String args[]){ //创建ObjectMapper对象 ObjectMapper mapper = new ObjectMapper(); //创建Student实体类 Student student = new Student(); //创建josn格式对象 String jsonString = "{\"name\":\"Mahesh\", \"age\":21}"; //map json to student try { //json转实体对象 Student student = mapper.readValue(jsonString, Student.class); System.out.println(student); //实体对象转换json jsonString = mapper.writeValueAsString(student); System.out.println(jsonString); } catch (JsonParseException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
- 输出结果为
-
Student [ name: Mahesh, age: 21 ] { "name" : "Mahesh", "age" : 21 }
chenyb 随笔记录,方便自己使用
2018-10-30