Java对象、Json、Xml转换工具Jackson使用


在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使用的内容,更多相关内容请关注编程之家(www.php.cn)!


版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念
xml文件介绍及使用
xml编程(一)-xml语法
XML文件结构和基本语法
第2章 包装类
XML入门的常见问题(二)
Java对象的强、软、弱和虚引用
JS解析XML文件和XML字符串详解
java中枚举的详细使用介绍
了解Xml格式
XML入门的常见问题(四)
深入SQLite多线程的使用总结详解
PlayFramework完整实现一个APP(一)
XML和YAML的使用方法
XML轻松学习总节篇