java操作json工具类

package com.zte.esb.iesb.directory.utils;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.zte.esb.iesb.directory.model.Directory;
import com.zte.ssb.framework.util.ApplicationGlobalResource;
import com.zte.ssb.servicecontainer.business.server.ISession;
import com.zte.ssb.servicecontainer.business.server.RIAContext;

@SuppressWarnings("unchecked")
public class JsonUtil
{
    private static ApplicationGlobalResource appresource;

    static
    {
        appresource = ApplicationGlobalResource.getInstance();
    }

    /**
     * json转换成list
     * 
     * @param <T>
     * @param json
     * @param type
     * @return
     */
    @SuppressWarnings("deprecation")
    public static <T> List<T> readJsonToList(String json, T type)
    {
        JSONArray j_a = JSONArray.fromObject(json);
        return JSONArray.toList(j_a, type.getClass());
    }

    /**
     * json转换成对象
     * 
     * @param <T>
     * @param json
     * @param type
     * @return
     */
    public static <T> T readJsonToBean(String json, T type)
    {
        JSONObject jo = JSONObject.fromObject(json);
        return (T) JSONObject.toBean(jo, type.getClass());
    }

    /**
     * list转换成json
     * 
     * @param <T>
     * @param list
     * @return
     */
    public static <T> String writeJson(List<T> list)
    {
        JSONArray j_a = JSONArray.fromObject(list);
        return j_a.toString();
    }

    /**
     * 对象转换成json
     * 
     * @param <T>
     * @param type
     * @return
     */
    public static <T> String writeJson(T type)
    {
        JSONObject jo = JSONObject.fromObject(type);
        return jo.toString();
    }

    public static void main(String[] args)
    {
        String json = "[{'id':'3','name':'宏观经济库服务','description':'宏观经济库相关的服务,如经济运行信息等'},{'id':'4','name':'GIS地理信息库服务','description':'GIS地理信息库相关的服务,如地图定位、地图查询等'},{'id':'8a925d963c222fe0013c224651cf0005','name':'aaaa','description':'ssss'},{'id':'8a925d963c222fe0013c224445460004','name':'中国','description':'天朝'},{'id':'8a925d963c222fe0013c224368a20003','name':'aaaa','description':'aaaaa'},{'id':'8a925d963c222fe0013c2247f4920008','name':'ok?','description':'yes'}]";
        readJsonToList(json, new Directory());
    }

   }
 
 
 
 
 
 
 
 

package com.zte.esb.iesb.utils;

import java.io.IOException; import java.util.LinkedHashMap; import java.util.List; import java.util.Map;

import org.codehaus.jackson.JsonEncoding; import org.codehaus.jackson.JsonGenerationException; import org.codehaus.jackson.JsonGenerator; import org.codehaus.jackson.JsonParseException; import org.codehaus.jackson.map.JsonMappingException; import org.codehaus.jackson.map.ObjectMapper;

public class JsonUtil {     private static ObjectMapper objectMapper = new ObjectMapper();     private static JsonGenerator jsonGenerator = JsonUtil.getJsonGenerator();

    private static JsonGenerator getJsonGenerator()     {         try         {             jsonGenerator = objectMapper.getJsonFactory().createJsonGenerator(System.out, JsonEncoding.UTF8);         }         catch (IOException e)         {             e.printStackTrace();         }         return jsonGenerator;     }

    /**      * 把java对象转换成json串      *      * @throws IOException      */     public static String writeEntity2Json(Object o) throws IOException     {         return objectMapper.writeValueAsString(o);     }

    /**      * 把List(List中可存放java对象)转换成json串      *      * @param list      * @return      * @throws IOException      */     @SuppressWarnings("unchecked")     public static String writeList2Json(List list) throws IOException     {         return objectMapper.writeValueAsString(list);     }

    /**      * 把map转换成json串      *      * @param map      * @return      */     @SuppressWarnings("unchecked")     public static String writeMap2Json(Map map)     {         try         {             return objectMapper.writeValueAsString(map);         }         catch (JsonGenerationException e)         {             e.printStackTrace();             return null;         }         catch (JsonMappingException e)         {             e.printStackTrace();             return null;         }         catch (IOException e)         {             e.printStackTrace();             return null;         }

    }

    /**      * JSON字符串转换为对象      */     @SuppressWarnings("unchecked")     public static Object readJson2Entity(String json, Class c)     {         try         {             return objectMapper.readValue(json, c);         }         catch (JsonParseException e)         {             e.printStackTrace();             return null;         }         catch (JsonMappingException e)         {             e.printStackTrace();             return null;         }         catch (IOException e)         {             e.printStackTrace();             return null;         }     }

    /**      * JSON转换为List对象      */     @SuppressWarnings("unchecked")     public static List<LinkedHashMap<String, Object>> readJson2List(String json)     {         try         {             return objectMapper.readValue(json, List.class);         }         catch (JsonParseException e)         {             e.printStackTrace();             return null;         }         catch (JsonMappingException e)         {             e.printStackTrace();             return null;         }         catch (IOException e)         {             e.printStackTrace();             return null;         }     }

    /**      * JSON转换为数组对象      */     public static Object[] readJson2Array(String json)     {         try         {             return objectMapper.readValue(json, Object[].class);         }         catch (JsonParseException e)         {             e.printStackTrace();             return null;         }         catch (JsonMappingException e)         {             e.printStackTrace();             return null;         }         catch (IOException e)         {             e.printStackTrace();             return null;         }     }

    /**      * JSON转换为Map对象      */     @SuppressWarnings("unchecked")     public static Map<String, Map<String, Object>> readJson2Map(String json)     {         try         {             return objectMapper.readValue(json, Map.class);         }         catch (JsonParseException e)         {             e.printStackTrace();             return null;         }         catch (JsonMappingException e)         {             e.printStackTrace();             return null;         }         catch (IOException e)         {             e.printStackTrace();             return null;         }     } }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值