Java对象、Json、Xml转换工具Jackson使用
在Java项目中將一个对象转换成一段Json格式的字符串是非常常见的,能够实现这种需求的工具包也比较多,例如Gson、JSON-lib、Jackson等等。本文主要介绍Jackson的使用,Jackson除了能实现Java对象与Json串的互转外,还能將Java对象转换为Xml格式,使用较为简单而且据说效率比较高。
对于Jackson的jar包我们可以从maven资源库中下载:http://mvnrepository.com/
所需jar包如下,按照名称搜索下载即可。
接下来编写测试用例,我们需要一个java类:
package com.csii.jackson.object;
public class Book{
private String name;
private int price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public Book() {
}
public Book(String name,int price) {
this.name = name;
this.price = price;
}
@Override
public String toString() {
return "name:" + name +"; price:" + price;
}
}
1.將Java对象转换为Json字符串:
@Test
public void testGenJson()
{
ObjectMapper objMapper = new ObjectMapper();
Book book = new Book("Think in Java",100);
try {
jsonGen = objMapper.getJsonFactory().createJsonGenerator(System.out,JsonEncoding.UTF8);
jsonGen.writeObject(book);
} catch (IOException e) {
e.printStackTrace();
}
}
运行测试方法,控制台输出:
{"name":"Think in Java","price":100}
2.將Json字符串转为Java对象:
/*
* Json转Java对象
*/
@Test
public void testGenObjByJson()
{
ObjectMapper objMapper = new ObjectMapper();
String json = "{\"name\":\"Think in Java\",\"price\":100}";
try {
Book book = objMapper.readValue(json, Book.class);
System.out.println(book);
} catch (IOException e) {
e.printStackTrace();
}
}
由于我们重写了Book类的toString方法,运行测试方法,控制台输出:
name:Think in Java; price:100
3.將Java对象转为Xml格式:
/*
* Java对象转xml
*/
@Test
public void testGenXml()
{
XmlMapper xmlMapper = new XmlMapper();
Book book = new Book("Think in Java",100);
try {
String xmlStr = xmlMapper.writeValueAsString(book);
System.out.println(xmlStr);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
运行测试方法,控制台输出:
<Book xmlns=""><name>Think in Java</name><price>100</price></Book>
4.將xml格式字符串转为Java对象:
/*
* xml转Java对象
*/
@Test
public void testGenObjByXml()
{
XmlMapper xmlMapper = new XmlMapper();
String xmlStr = "<Book><name>Think in Java</name><price>100</price></Book>";
try {
Book book = xmlMapper.readValue(xmlStr, Book.class);
System.out.println(book);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
输出内容:
name:Think in Java; price:100
完整的测试用例代码:
package com.csii.jackson.test;
import java.io.IOException;
import org.junit.Test;
import com.csii.jackson.object.Book;
import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
@SuppressWarnings("deprecation")
public class JsonTest {
private JsonGenerator jsonGen = null;
/*
* Java对象转 Json
*/
@Test
public void testGenJson()
{
ObjectMapper objMapper = new ObjectMapper();
Book book = new Book("Think in Java",100);
try {
jsonGen = objMapper.getJsonFactory().createJsonGenerator(System.out,JsonEncoding.UTF8);
jsonGen.writeObject(book);
} catch (IOException e) {
e.printStackTrace();
}
}
/*
* Json转Java对象
*/
@Test
public void testGenObjByJson()
{
ObjectMapper objMapper = new ObjectMapper();
String json = "{\"name\":\"Think in Java\",\"price\":100}";
try {
Book book = objMapper.readValue(json, Book.class);
System.out.println(book);
} catch (IOException e) {
e.printStackTrace();
}
}
/*
* Java对象转xml
*/
@Test
public void testGenXml()
{
XmlMapper xmlMapper = new XmlMapper();
Book book = new Book("Think in Java",100);
try {
String xmlStr = xmlMapper.writeValueAsString(book);
System.out.println(xmlStr);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
/*
* xml转Java对象
*/
@Test
public void testGenObjByXml()
{
XmlMapper xmlMapper = new XmlMapper();
String xmlStr = "<Book><name>Think in Java</name><price>100</price></Book>";
try {
Book book = xmlMapper.readValue(xmlStr, Book.class);
System.out.println(book);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}