成都网站建设设计

将想法与焦点和您一起共享

如何使用Jackson工具对Java对象、Json和Xml进行转换

如何使用Jackson工具对Java对象、Json和Xml进行转换?针对这个问题,这篇文章给出了相对应的分析和解答,希望能帮助更多想解决这个问题的朋友找到更加简单易行的办法。

发展壮大离不开广大客户长期以来的信赖与支持,我们将始终秉承“诚信为本、服务至上”的服务理念,坚持“二合一”的优良服务模式,真诚服务每家企业,认真做好每个细节,不断完善自我,成就企业,实现共赢。行业涉及电动窗帘等,在成都网站建设成都营销网站建设、WAP手机网站、VI设计、软件开发等项目上具有丰富的设计经验。

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

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

如何使用Jackson工具对Java对象、Json和Xml进行转换

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

运行测试方法,控制台输出:

Think in Java100

4.將xml格式字符串转为Java对象:

    /*
     * xml转Java对象
     */
    @Test    public void testGenObjByXml()
    {
        XmlMapper xmlMapper = new XmlMapper();
        String xmlStr = "Think in Java100"; 
        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 = "Think in Java100"; 
        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包如下,按照名称搜索下载即可。

如何使用Jackson工具对Java对象、Json和Xml进行转换

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

运行测试方法,控制台输出:

Think in Java100

4.將xml格式字符串转为Java对象:

    /*
     * xml转Java对象
     */
    @Test    public void testGenObjByXml()
    {
        XmlMapper xmlMapper = new XmlMapper();
        String xmlStr = "Think in Java100"; 
        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 = "Think in Java100"; 
        try {
            Book book = xmlMapper.readValue(xmlStr, Book.class);
            System.out.println(book);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

以上就是使用Jackson工具对Java对象、Json和Xml进行转换的方法,详细使用情况还需要大家自己亲自动手使用过才能领会。如果想了解更多相关内容,欢迎关注创新互联行业资讯频道!


分享文章:如何使用Jackson工具对Java对象、Json和Xml进行转换
地址分享:http://chengdu.cdxwcx.cn/article/jiepdp.html