[Android学习笔记]jackson库的使用

Jackson库一般用于序列化和反序列化操作,通常会涉及到的操作是:

1. Java Object -> Json String

2. Java Object -> Xml String

3. Json String -> Java Object

4. Xml String -> Java Object

 

JSON相关操作需要jar包:jackson-all-x.x.x.jar

http://jackson.codehaus.org/

 

XML相关操作需要jar包:

要在Android下使用Jackson,则需要:

jackson-dataformat-android

aalto-xml

stax-api

stax2-api

4个jar包

下载地址:

jackson-dataformat-android

stax-api&stax2-api

aalto-xml

 

在此可下载所有相关jar包:

http://pan.baidu.com/s/1pJ9DyGV


 

Ex:

SerializationHelper.java

package com.example.utils;

import java.io.IOException;

import com.fasterxml.aalto.stax.InputFactoryImpl;
import com.fasterxml.aalto.stax.OutputFactoryImpl;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.JacksonXmlModule;
import com.fasterxml.jackson.dataformat.xml.XmlFactory;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;

public class SerializationHelper
{
    private static ObjectMapper objectMapper = new ObjectMapper();

    /**
     * Java Object to JSON String
     * 
     * @param object
     * @return
     */
    public static String ObjectToJson(Object object)
    {
        String result = null;

        try
        {
            result = objectMapper.writeValueAsString(object);

        } catch (JsonProcessingException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return result;
    }

    /** JSON to Java Object
     * 
     * @param jsonString
     * @param clazz
     * @return
     */
    public static <T> T JsonToObject(String jsonString, Class<T> clazz)
    {
        T result = null;

        try
        {
            result = objectMapper.readValue(jsonString, clazz);
            
        } catch (JsonParseException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JsonMappingException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        
        return result;
    }
    
    

    /**
     * Java Object to XML String
     * 
     * @param object
     * @return
     */
    public static String ObjectToXml(Object object)
    {
        String result = null;

        XmlFactory factory = new XmlFactory(new InputFactoryImpl(), new OutputFactoryImpl());

        JacksonXmlModule module = new JacksonXmlModule();

        XmlMapper mapper = new XmlMapper(factory, module);
        
        try
        {
            result = mapper.writeValueAsString(object);
        } catch (JsonProcessingException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return result;
    }
    
    /** XML to Java Object
     * 
     * @param xml
     * @param clazz
     * @return
     */
    public static <T> T XmlToObject(String xml, Class<T> clazz)
    {
        T result = null;

        XmlFactory factory = new XmlFactory(new InputFactoryImpl(), new OutputFactoryImpl());
        JacksonXmlModule module = new JacksonXmlModule();
        XmlMapper mapper = new XmlMapper(factory, module);
        
        try
        {
            result = mapper.readValue(xml, clazz);
        } catch (JsonParseException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JsonMappingException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        return result;
    }
}
SerializationHelper

 

MainActivity.java

@Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        Student st = new Student();
        st.setName("stephen lee");
        st.setAge(123);
        st.setId(1);
        
        String jsonString = SerializationHelper.ObjectToJson(st);
        String xmlString = SerializationHelper.ObjectToXml(st);

        Toast.makeText(this, jsonString, Toast.LENGTH_SHORT).show();
        Toast.makeText(this, xmlString, Toast.LENGTH_SHORT).show();
        
        Student stFromJson = SerializationHelper.JsonToObject(jsonString, Student.class);
        Student stFromXml = SerializationHelper.XmlToObject(xmlString, Student.class);
        
        Toast.makeText(this, "stFromJson:" + stFromJson.getName(), Toast.LENGTH_SHORT).show();
        Toast.makeText(this, "stFromXml:" + stFromXml.getName(), Toast.LENGTH_SHORT).show();
    }
MainActivity.java

 

 

 

转载于:https://www.cnblogs.com/hellenism/p/3643087.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值