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类:

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类:

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
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板