zoukankan      html  css  js  c++  java
  • json格式的一些常用操作方法

      1 package com.liveyc.restfull.until;
      2 
      3 import java.util.HashMap;
      4 import java.util.Iterator;
      5 import java.util.List;
      6 
      7 import org.apache.commons.logging.Log;
      8 import org.apache.commons.logging.LogFactory;
      9 import org.codehaus.jackson.map.ObjectMapper;
     10 import org.codehaus.jackson.type.JavaType;
     11 
     12 import net.sf.json.JSONArray;
     13 import net.sf.json.JSONObject;
     14 
     15 public class JsonTools {
     16     
     17     public static Log log = LogFactory.getLog(JsonTools.class);
     18     
     19     private static ObjectMapper objectMapper = new ObjectMapper();
     20     /**
     21      * 
     22      * @author JSON工具类
     23      * @param 
     24      * 
     25      */
     26     
     27 
     28     /***
     29      * 将对象序列化为JSON文本
     30      * @param object
     31      * @return
     32      */
     33     public static String toJSONString(Object object)
     34     {
     35         JSONArray jsonArray = JSONArray.fromObject(object);
     36 
     37         return jsonArray.toString();
     38     }
     39     
     40     /***
     41      * 将JSON对象序列化为JSON文本
     42      * @param jsonObject
     43      * @return
     44      */
     45     public static String toObjString(Object object)
     46     {
     47         JSONObject jsonObject = JSONObject.fromObject(object);
     48         return jsonObject.toString();
     49     } 
     50     
     51     /***
     52      * 将对象转换为JSON对象数组
     53      * @param object
     54      * @return
     55      */
     56     public static JSONArray toJSONArray(Object object)
     57     {
     58         return JSONArray.fromObject(object);
     59     }
     60 
     61     /***
     62      * 将对象转换为JSON对象
     63      * @param object
     64      * @return
     65      */
     66     public static JSONObject toJSONObject(Object object)
     67     {
     68         return JSONObject.fromObject(object);
     69     }
     70 
     71     /***
     72      * 将对象转换为HashMap
     73      * @param object
     74      * @return
     75      */
     76     public static HashMap toHashMap(Object object)
     77     {
     78         HashMap<String, Object> data = new HashMap<String, Object>();
     79         JSONObject jsonObject = JsonTools.toJSONObject(object);
     80         Iterator it = jsonObject.keys();
     81         while (it.hasNext())
     82         {
     83             String key = String.valueOf(it.next());
     84             Object value = jsonObject.get(key);
     85             data.put(key, value);
     86         }
     87 
     88         return data;
     89     }
     90 
     91 
     92     
     93 
     94 
     95   
     96     public static <T> T json2Bean(String json, Class<T> beanClass) {  
     97         try {  
     98             return objectMapper.readValue(json, beanClass);  
     99         } catch (Exception e) {  
    100             log.error(e);
    101             new Exception("解析参数出错");
    102         }  
    103         return null;  
    104     }  
    105 
    106     
    107     public static <T> List<T> json2List(String json, Class<T> beanClass) {  
    108         try {  
    109             return (List<T>)objectMapper.readValue(json, getCollectionType(List.class, beanClass));  
    110         } catch (Exception e) {  
    111             e.printStackTrace();  
    112         }  
    113         return null;  
    114     }  
    115     
    116     public static String getJsonFromObject(Object obj) {
    117         try {
    118             return objectMapper.writeValueAsString(obj);
    119         } catch (Exception e) {
    120             e.printStackTrace();
    121         }
    122         return null;
    123     }
    124     
    125     public static JavaType getCollectionType(Class<?> collectionClass, Class<?>... elementClasses) {     
    126         return objectMapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);     
    127     }   
    128 }
  • 相关阅读:
    ios10兼容问题
    safari图片跨域
    出现Unable to locate appropriate constructor on class 错误可能的原因
    localStorage、sessionStorage用法以及区别
    多行文本垂直居中,多行文本溢出
    【汉字】转【pīnyīn】
    移除行块级元素之间的空格(译文)
    jquery插件——检测DOM元素是否在浏览器可视范围之内
    监控阮一峰老师的blog
    PHP实现linux命令tail -f
  • 原文地址:https://www.cnblogs.com/xuyou551/p/8028289.html
Copyright © 2011-2022 走看看