您的位置:首页 > Web前端 > JavaScript

Json转换

2015-11-24 23:00 567 查看
package com.may.syt.util;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

public class JsonConverter {
private ObjectMapper om ;
private static JsonConverter jc = new JsonConverter() ;
public static Logger logger = Logger.getLogger("AMMSS");
private JsonConverter()
{
om = new ObjectMapper() ;
}

public static JsonConverter getInstance()
{
return jc ;
}

public String getNowTime()
{
SimpleDateFormat sdf = new SimpleDateFormat( "yyyyMMddHHmmss" ) ;
Date curDate = new Date();

return sdf.format(curDate);
}

public String convertTime(String srcTime)
{
String dstTime = "";

if(srcTime == null || srcTime.isEmpty())
return "";

dstTime = srcTime.replace("-", "");

return dstTime;
}

public int convertStrToInt(String s)
{
try
{
int n = Integer.parseInt(s);

return n;
}
catch(Exception ex)
{

}

return -1;
}

/**
* @name obj2Str
* @todo 将对象转换成json字符串
* @param o :需要转换的对象
* @return json字符串
*/
public <T> String obj2Str(T o)
{
String result = null ;

try
{
result = om.writeValueAsString(o) ;
}
catch (JsonGenerationException e)
{
logger.info( "NJGL#"+UuidGenerator.getUUID()+"#*#[obj2Str:JsonGenerationException]-->" + o.toString() + ";"+ e.getMessage()+"#2") ;
}
catch (JsonMappingException e)
{
logger.info( "NJGL#"+UuidGenerator.getUUID()+"#*#[obj2Str:JsonMappingException]-->" + o.toString() + ";"+ e.getMessage() +"#2") ;
}
catch (IOException e)
{
logger.info( "NJGL#"+UuidGenerator.getUUID()+"#*#[obj2Str:IOException]-->" + o.toString() + ";"+ e.getMessage() +"#2") ;
}

return result ;
}

/**
* @name list2Str
* @todo 将链表转换成json字符串
* @param list :需要转换的链表
* @return json字符串
*/
public <T> String list2Str(List<T> list)
{
String result = null ;

try
{
result = om.writeValueAsString(list) ;
}
catch (JsonGenerationException e)
{
logger.info( "NJGL#"+UuidGenerator.getUUID()+"#*#[list2Str:JsonGenerationException]-->" + list.toString() + ";"+ e.getMessage() +"#2") ;
}
catch (JsonMappingException e)
{
logger.info( "NJGL#"+UuidGenerator.getUUID()+"#*#[list2Str:JsonMappingException]-->" + list.toString() + ";"+ e.getMessage() +"#2") ;
}
catch (IOException e)
{
logger.info( "NJGL#"+UuidGenerator.getUUID()+"#*#[list2Str:IOException]-->" + list.toString() + ";"+ e.getMessage() +"#2") ;
}

return result ;
}

/**
* @name map2Str
* @todo 将映射表转换成json字符串
* @param map :需要转换的映射表
* @return json字符串
*/
public <T, E> String map2Str(Map<T, E> map)
{
String result = null ;

try
{
result = om.writeValueAsString(map) ;
}
catch (JsonGenerationException e)
{
logger.info( "NJGL#"+UuidGenerator.getUUID()+"#*#[map2Str:JsonGenerationException]-->" + map.toString() + ";"+ e.getMessage()+"#2" ) ;
}
catch (JsonMappingException e)
{
logger.info( "NJGL#"+UuidGenerator.getUUID()+"#*#[map2Str:JsonMappingException]-->" + map.toString() + ";"+ e.getMessage()+"#2" ) ;
}
catch (IOException e)
{
logger.info( "NJGL#"+UuidGenerator.getUUID()+"#*#[map2Str:IOException]-->" + map.toString() + ";"+ e.getMessage()+"#2" ) ;
}

return result ;
}

/**
* @name str2Obj
* @todo 将json字符串转换成对象
* @param 	str	:	json字符串
* 			c	:	T的class
* @return 对象
*/
public <T> T str2Obj(String str, Class<T> c)
{
if (str == null || str == "")
{
return null ;
}

T result = null ;

try
{
result = (T) om.readValue(str, c) ;
}
catch (JsonParseException e)
{
logger.info( "NJGL#"+UuidGenerator.getUUID()+"#*#[str2Obj:JsonParseException]-->" + str + ";" + c.toString() + ";"+ e.getMessage() +"#2") ;
}
catch (JsonMappingException e)
{
logger.info( "NJGL#"+UuidGenerator.getUUID()+"#*#[str2Obj:JsonMappingException]-->" + str + ";" + c.toString() + ";"+ e.getMessage()+"#2" ) ;
}
catch (IOException e)
{
logger.info( "NJGL#"+UuidGenerator.getUUID()+"#*#[str2Obj:IOException]-->" + str + ";" + c.toString() + ";"+ e.getMessage() +"#2") ;
}

return result ;
}

/**
* @name str2List
* @todo 将json字符串转换成链表
* @param 	str	:	json字符串
* 			c	:	T的class
* @return 链表
*/
public <T> List<T> str2List(String str, Class<T> c)
{
List<T> list = null ;

try
{
T[] tArray = (T[]) om.readValue(str, c);
list = Arrays.asList(tArray) ;
}
catch (JsonParseException e)
{
logger.info( "NJGL#"+UuidGenerator.getUUID()+"#*#[str2List:JsonParseException]-->" + str + ";" + c.toString() + ";"+ e.getMessage() +"#2") ;
}
catch (JsonMappingException e)
{
logger.info( "NJGL#"+UuidGenerator.getUUID()+"#*#[str2List:JsonMappingException]-->" + str + ";" + c.toString() + ";"+ e.getMessage() +"#2") ;
}
catch (IOException e)
{
logger.info( "NJGL#"+UuidGenerator.getUUID()+"#*#[str2List:IOException]-->" + str + ";" + c.toString() + ";"+ e.getMessage() +"#2") ;
}

return list ;
}

/**
* @name str2Array
* @todo 将json字符串转换成数组
* @param 	str	:	json字符串
* 			c	:	T的class
* @return 数组
*/
@SuppressWarnings("unchecked")
public <T> T[] str2Array(String str, Class<T> c)
{
T[] array = null ;

try
{
array = (T[]) om.readValue(str, c) ;
}
catch (JsonParseException e)
{
logger.info( "NJGL#"+UuidGenerator.getUUID()+"#*#[str2Array:JsonParseException]-->" + str + ";" + c.toString() + ";"+ e.getMessage() +"#2") ;
}
catch (JsonMappingException e)
{
logger.info( "NJGL#"+UuidGenerator.getUUID()+"#*#[str2Array:JsonMappingException]-->" + str + ";" + c.toString() + ";"+ e.getMessage() +"#2") ;
}
catch (IOException e)
{
logger.info( "NJGL#"+UuidGenerator.getUUID()+"#*#[str2Array:IOException]-->" + str + ";" + c.toString() + ";"+ e.getMessage() +"#2") ;
}

return array ;
}

/**
* @name str2Map
* @todo 将json字符串转换成映射表
* @param 	str	:	json字符串
* 			c	:	T的class
* @return 映射表
*/
@SuppressWarnings("unchecked")
public <T, E> Map<T, E> str2Map(String str)
{
Map<T, E> map = null ;

try
{
map = om.readValue(str, Map.class) ;
}
catch (JsonParseException e)
{
logger.info( "NJGL#"+UuidGenerator.getUUID()+"#*#[str2Map:JsonParseException]-->" + str + ";" + Map.class.toString() + ";"+ e.getMessage() +"#2") ;
}
catch (JsonMappingException e)
{
logger.info( "NJGL#"+UuidGenerator.getUUID()+"#*#[str2Map:JsonMappingException]-->" + str + ";" + Map.class.toString() + ";"+ e.getMessage() +"#2") ;
}
catch (IOException e)
{
logger.info( "NJGL#"+UuidGenerator.getUUID()+"#*#[str2Map:IOException]-->" + str + ";" + Map.class.toString() + ";"+ e.getMessage() +"#2") ;
}
return map ;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  JSON转换