Java物件、Json、Xml轉換工具Jackson使用

黄舟
發布: 2017-02-22 15:02:36
原創
3680 人瀏覽過


在Java项目中將一个对象转换成一段Json格式的字符串是非常常见的,能够实现这种需求的工具包也比较多,例如Gson、JSON-lib、Jackson等等。本文主要介绍Jackson的使用,Jackson除了能实现Java对象与Json串的互转外,还能將Java对象转换为Xml格式,使用较为简单而且据说效率比较高。
对于Jackson的jar包我们可以从maven资源库中下载:http://www.php.cn/

所需jar包如下,按照名称搜索下载即可。

Java物件、Json、Xml轉換工具Jackson使用

接下来编写测试用例,我们需要一个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();
        }

    }
}
登入後複製
登入後複製

在Java项目中將一个对象转换成一段Json格式的字符串是非常常见的,能够实现这种需求的工具包也比较多,例如Gson、JSON-lib、Jackson等等。本文主要介绍Jackson的使用,Jackson除了能实现Java对象与Json串的互转外,还能將Java对象转换为Xml格式,使用较为简单而且据说效率比较高。
对于Jackson的jar包我们可以从maven资源库中下载:http://www.php.cn/

所需jar包如下,按照名称搜索下载即可。

Java物件、Json、Xml轉換工具Jackson使用

接下来编写测试用例,我们需要一个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();
        }

    }
}
登入後複製
登入後複製

以上就是Java对象、Json、Xml转换工具Jackson使用的内容,更多相关内容请关注PHP中文网(www.php.cn)!


相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板