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 字符串 与 java 对象的转换
- Java中将JSON格式的数据转换成对应的Bean、Map、List数据
- 做图服务器测试脚本——实习笔记
- Jackson 框架使用,JSON转换
- JSON-lib框架,JSON转换、XML转换
- java对象和json的互转
- JSONObject.fromObject(obj)顺序被打乱的异常解决方法
- 将实体转化成json并将json转换成DataTable
- Json字符串转excel表格文件
- serializeArray()与 serialize()
- json转换,对象转json字符串或json字符串转对象
- JSON类型数据转换为对象,并排除指定的属性.JAVA将购物车数据写入到cookie中
- Spring Boot : 自动JSON转换和热部署(二)
- json,字符串, 对象转换
- http://www.cnblogs.com/hitwtx/articles/2468633.html
- JSONObject.fromObject - JSON与对象的转换
- net.sf.json 转换JSON
- 重温jsp①
- jsp练习1
- js实现接收表单的值并将值拼在表单action后面的方法