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

时间:2021-12-18 05:27:30

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