Jackson可以轻松的将Java对象转换成json对象和xml文档,同样也可以将json、xml转换成Java对象。相比json-lib框架,Jackson所依赖的jar包较少,简单易用并且性能也要相对高些。而且Jackson社区相对比较活跃,更新速度也比较快。
一、
准备工作
pom.xml 里关联jar包如下:
<
project
xmlns=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<
modelVersion
>4.0.0
</
modelVersion
>
<
groupId
>com.waegg
</
groupId
>
<
artifactId
>jacksonTest
</
artifactId
>
<
version
>0.0.1-SNAPSHOT
</
version
>
<
dependencies
>
<
dependency
>
<
groupId
>junit
</
groupId
>
<
artifactId
>junit
</
artifactId
>
<
version
>4.12
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>org.codehaus.jackson
</
groupId
>
<
artifactId
>jackson-mapper-asl
</
artifactId
>
<
version
>1.9.13
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>com.fasterxml.jackson.dataformat
</
groupId
>
<
artifactId
>jackson-dataformat-xml
</
artifactId
>
<
version
>2.7.1
</
version
>
</
dependency
>
</
dependencies
>
<
packaging
>war
</
packaging
>
</
project
>
二、Bean代码
AccountBean :
package com.bugyun.pojo;
public
class AccountBean {
private
int id;
private String name;
private String email;
private String address;
private Birthday birthday;
@Override
public String toString() {
return
this.name +
"#" +
this.id +
"#" +
this.address +
"#" +
this.birthday +
"#" +
this.email;
public
int getId() {
return id;
public
void setId(
int id) {
this.id = id;
public String getName() {
return name;
public
void setName(String name) {
this.name = name;
public String getEmail() {
return email;
public
void setEmail(String email) {
this.email = email;
public String getAddress() {
return address;
public
void setAddress(String address) {
this.address = address;
public Birthday getBirthday() {
return birthday;
public
void setBirthday(Birthday birthday) {
this.birthday = birthday;
Birthday:
package com.bugyun.pojo;
public
class Birthday {
private String birthday;
public Birthday(String birthday) {
super();
this.birthday = birthday;
public Birthday() {
public String getBirthday() {
return birthday;
public
void setBirthday(String birthday) {
this.birthday = birthday;
@Override
public String toString() {
return
this.birthday;
import org.codehaus.jackson.JsonEncoding;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.bugyun.pojo.AccountBean;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
public
class JacksonTest {
private JsonGenerator jsonGenerator =
null;
private ObjectMapper objectMapper =
null;
private AccountBean bean =
null;
@Before
public
void init() {
bean =
new AccountBean();
bean.setAddress(
"js_wuxi");
bean.setEmail(
"bugyun@hotmail.com");
bean.setId(
1);
bean.setName(
"bugyun");
objectMapper =
new ObjectMapper();
try {
jsonGenerator = objectMapper.getJsonFactory().createJsonGenerator(System.out, JsonEncoding.UTF8);
}
catch (IOException e) {
e.printStackTrace();
@After
public
void destory() {
try {
if (jsonGenerator !=
null) {
jsonGenerator.flush();
if (!jsonGenerator.isClosed()) {
jsonGenerator.close();
jsonGenerator =
null;
objectMapper =
null;
bean =
null;
System.gc();
}
catch (IOException e) {
e.printStackTrace();
@Test
public
void writeEntityJSON() {
try {
System.out.println(
"jsonGenerator");
jsonGenerator.writeObject(bean);
System.out.println(
"\nObjectMapper");
objectMapper.writeValue(System.out, bean);
}
catch (IOException e) {
e.printStackTrace();
@Test
public
void writeMapJSON() {
try {
Map<String, Object> map =
new HashMap<String, Object>();
map.put(
"name", bean.getName());
map.put(
"account", bean);
System.out.println(
"jsonGenerator");
jsonGenerator.writeObject(map);
System.out.println(
"\n ObjectMapper");
objectMapper.writeValue(System.out, map);
}
catch (IOException e) {
e.printStackTrace();
@Test
public
void writeListJSON() {
try {
List<AccountBean> list =
new ArrayList<AccountBean>();
list.add(bean);
System.out.println(
"jsonGenerator");
jsonGenerator.writeObject(list);
System.out.println(
"\nObjectMapper");
System.out.println(
"1###" + objectMapper.writeValueAsString(list));
System.out.print(
"2###");
objectMapper.writeValue(System.out, list);
}
catch (IOException e) {
e.printStackTrace();
@Test
public
void writeOthersJSON() {
try {
String[] arr = {
"a",
"b",
"c" };
jsonGenerator.writeStartObject();
jsonGenerator.writeObjectFieldStart(
"user");
jsonGenerator.writeStringField(
"name",
"jackson");
jsonGenerator.writeBooleanField(
"sex",
true);
jsonGenerator.writeNumberField(
"age",
22);
jsonGenerator.writeEndObject();
jsonGenerator.writeArrayFieldStart(
"infos");
jsonGenerator.writeNumber(
22);
jsonGenerator.writeString(
"this is array");
jsonGenerator.writeEndArray();
jsonGenerator.writeEndObject();
AccountBean bean =
new AccountBean();
bean.setAddress(
"address");
bean.setEmail(
"email");
bean.setId(
1);
bean.setName(
"haha");
jsonGenerator.writeStartObject();
jsonGenerator.writeObjectField(
"user", bean);
jsonGenerator.writeObjectField(
"infos", arr);
jsonGenerator.writeEndObject();
}
catch (Exception e) {
e.printStackTrace();
@Test
public
void readJson2Entity() {
String json =
"{\"address\":\"address\",\"name\":\"haha\",\"id\":1,\"email\":\"email\"}";
try {
AccountBean acc = objectMapper.readValue(json, AccountBean.
class);
System.out.println(acc.getName());
System.out.println(acc);
}
catch (JsonParseException e) {
e.printStackTrace();
}
catch (JsonMappingException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
@Test
public
void readJson2List() {
String json =
"[{\"address\": \"address2\",\"name\":\"haha2\",\"id\":2,\"email\":\"email2\"},"
+
"{\"address\":\"address\",\"name\":\"haha\",\"id\":1,\"email\":\"email\"}]";
try {
List<Map<String, Object>> list = objectMapper.readValue(json, List.
class);
for (
int i =
0; i < list.size(); i++) {
Map<String, Object> map = list.get(i);
Set<String> set = map.keySet();
for (Iterator it = set.iterator(); it.hasNext();) {
String key = (String) it.next();
System.out.println(key +
":" + map.get(key));
}
catch (JsonParseException e) {
e.printStackTrace();
}
catch (JsonMappingException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
@Test
public
void readJson2Array() {
String json =
"[{\"address\": \"address2\",\"name\":\"haha2\",\"id\":2,\"email\":\"email2\"},"
+
"{\"address\":\"address\",\"name\":\"haha\",\"id\":1,\"email\":\"email\"}]";
try {
AccountBean[] arr = objectMapper.readValue(json, AccountBean[].
class);
for (
int i =
0; i < arr.length; i++) {
System.out.println(arr[i]);
}
catch (JsonParseException e) {
e.printStackTrace();
}
catch (JsonMappingException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
@Test
public
void readJson2Map() {
String json =
"{\"success\":true,\"A\":{\"address\": \"address2\",\"name\":\"haha2\",\"id\":2,\"email\":\"email2\"},"
+
"\"B\":{\"address\":\"address\",\"name\":\"haha\",\"id\":1,\"email\":\"email\"}}";
try {
Map<String, Map<String, Object>> maps = objectMapper.readValue(json, Map.
class);
Set<String> key = maps.keySet();
Iterator<String> iter = key.iterator();
while (iter.hasNext()) {
String field = iter.next();
System.out.println(field +
":" + maps.get(field));
}
catch (JsonParseException e) {
e.printStackTrace();
}
catch (JsonMappingException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
@Test
public
void writeObject2Xml() {
XmlMapper xml =
new XmlMapper();
try {
StringWriter sw =
new StringWriter();
xml.writeValue(sw, bean);
List<AccountBean> list =
new ArrayList<AccountBean>();
list.add(bean);
System.out.println(xml.writeValueAsString(list));
Map<String, AccountBean> map =
new HashMap<String, AccountBean>();
map.put(
"A", bean);
System.out.println(xml.writeValueAsString(map));
}
catch (JsonGenerationException e) {
e.printStackTrace();
}
catch (JsonMappingException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
引用原文:
http://bugyun.iteye.com/blog/2278997
写博客是为了记住自己容易忘记的东西,另外也是对自己工作的总结,文章可以转载,无需版权。希望尽自己的努力,做到更好,大家一起努力进步!
如果有什么问题,欢迎大家一起探讨,代码如有问题,欢迎各位大神指正!
给自己的梦想添加一双翅膀,让它可以在天空中自由自在的飞翔!