ObjectMapper类是Jackson库的主要类。它提供一些功能将转换成Java对象匹配JSON结构,反之亦然。它使用JsonParser和JsonGenerator的实例实现JSON实际的读/写。
类声明
以下是org.codehaus.jackson.map.ObjectMapper类的声明:
public class ObjectMapper
extends ObjectCodec
implements Versioned
嵌套类S.N.
类 & 描述
1
static class ObjectMapper.DefaultTypeResolverBuilder//定制TypeResolverBuilder,提供所谓的“默认输入”使用类型解析构建器(见enableDefaultTyping()了解详细信息)。
2
static class ObjectMapper.DefaultTyping//使用enableDefaultTyping()枚举指定什么样的类型(类)默认输入应该使用。
构造函数
S.N.
构造函数 & 描述
1
ObjectMapper()//默认的构造函数,这将构建默认JsonFactory必要时使用StdSerializerProvider作为其SerializerProvider,并BeanSerializerFactory作为其SerializerFactory。
2
ObjectMapper(JsonFactory jf)//构造使用指定的JsonFactory构建必要的JsonParsers和/或JsonGenerators映射。
3
ObjectMapper(JsonFactory jf, SerializerProvider sp, DeserializerProvider dp)
4
ObjectMapper(JsonFactory jf, SerializerProvider sp, DeserializerProvider dp, SerializationConfig sconfig, DeserializationConfig dconfig)
5
ObjectMapper(SerializerFactory sf) 不推荐使用。使用其他构造来代替; 注意,可以设置序列化工厂setSerializerFactory(org.codehaus.jackson.map.SerializerFactory)
这个类继承了以下类方法:
java.lang.Object
例子
测试类基本代码如下
/*
* @project java
* @package
* @file Jackson.java
* @version 1.0
*/
public class Jackson {
/*
*
* Class Descripton goes here.
*
* @class Jackson
* @version 1.0
*/
public static JsonGenerator jsonGenerator = null;
private static ObjectMapper mapper = new ObjectMapper();
public static void main(String[] args) {
Student student = new Student();
student.setIsstudent(true);
student.setUid(1000);
student.setUname("xiao liao");
student.setUpwd("123");
student.setNumber(12);
Map stuMap = new HashMap();
stuMap.put("1", student);
stuMap.put("2", student);
List stuList = new ArrayList();
List stuList1 = new ArrayList();
stuList1.add(student);
student= new Student();
student.setIsstudent(false);
student.setUid(200);
student.setUname("xiao mi");
stuList1.add(student);
stuList.add(student);
stuList.add("xiao xin");
stuList.add("xiao er");
stuList.add(stuMap);
//readJson2List();
try {
//readJson2Array();
//writeArray2Json(array);
//writeJson2List();
//writeEntity2Json(student);
writeJson2Entity();
//writeMap2Json(stuMap);
//writeList2Json(stuList1);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
*
* writeEntity2Json
* @description: TODO(实体类转换成json)
* @param object
* @throws IOException
*/
public static void writeEntity2Json(Object object) throws IOException {
mapper.writeValue( new File("D:\\developSoft\\aaadowload\\testjson1\\lib\\aa.txt"),object );
mapper.writeValue( System.out,object );
}
/**
*
* writeArray2Json
* @description: TODO(数组转换成json数组)
* @param object
* @throws IOException
*/
public static void writeArray2Json(Object object) throws IOException {
// writeValue具有和writeObject相同的功能
mapper.writeValue( new File("D:\\developSoft\\aaadowload\\testjson1\\lib\\aa.txt"),object );
mapper.writeValue(System.out,object );
}
/**
*
* writeMap2Json
* @description: TODO(map对象转换成json对象)
* @param object
* @throws IOException
* @since 2011-11-8 廖益平
*/
public static void writeMap2Json(Object object) throws IOException {
System.out.println("使用ObjectMapper-----------");
// writeValue具有和writeObject相同的功能
System.out.println("==>"+mapper.writeValueAsString(object));
mapper.writeValue( new File("D:\\developSoft\\aaadowload\\testjson1\\lib\\aamap.txt"),object );
mapper.writeValue( System.out , object );
}
/**
*
* writeList2Json
* @description: TODO(list转换成json)
* @param object
* @throws IOException
*/
public static void wriИ(CWWB+vb"GjW(