JsonUtil实例

package com.mylearn.util.json;


import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.PropertyFilter;


import java.util.*;


public class JsonUtil {






    /**
     * 将对象转换成字符串
     *
     * @param object bean 对象
     * @return
     * @throws Exception
     */
    public static String objectToJson(Object object) throws Exception {
        //首先构造过滤null的条件
        JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.setJsonPropertyFilter(new PropertyFilter() {
            public boolean apply(Object source, String name, Object value) {
                if (value == null) {// 这里是过滤的关键
                    return true;
                }
                return false;
            }
        });
        JSONObject jo = JSONObject.fromObject(object, jsonConfig);
        return jo.toString();
    }


    /**
     * 将json字符串转换对象
     *
     * @param paramJson json串
     * @param c         class对象
     * @param <T>       目标对象类名
     * @return
     * @throws Exception
     */
    public static <T> T jsonToObject(String paramJson, Class<T> c) throws Exception {
        JSONObject jsonObject = JSONObject.fromObject(paramJson);
        Object object = JSONObject.toBean(jsonObject, c);
        return (T) object;
    }


    public static Object[] jsonToObjectArray(String paramJson, Class c) throws Exception {
        JSONArray jsonArr = JSONArray.fromObject(paramJson);


        Object[] obj = new Object[jsonArr.size()];
        for (int j = 0; j < jsonArr.size(); j++) {
            JSONObject jsonObject = jsonArr.getJSONObject(j);
            obj[j] = JSONObject.toBean(jsonObject, c);
        }
        return obj;
    }


    /**
     *
     * @param list
     * @param <T>
     * @return
     */
    public static <T> String collection2Json(Collection<T> list) {
        JSONArray jsonArray = JSONArray.fromObject(list);
        return jsonArray.toString();
    }


    /**
     *
     * @param json
     * @param c
     * @param <T>
     * @return
     */
    public static <T> Collection<T> json2Collection(String json, Class<T> c) {
        JSONArray jsonArray = JSONArray.fromObject(json);
        Collection collection = JSONArray.toCollection(jsonArray, c);
        return (Collection<T>) collection;
    }




    /**
     * map转换为json
     * @param map
     * @param <K>
     * @param <V>
     * @return
     */
    public static <K, V> String map2Json(Map<K,V> map) {
        JSONObject jsonObject = JSONObject.fromObject(map);
          return jsonObject.toString();
      }


    /**
     * json转换为map
     * @param json
     * @param <K>
     * @param <V>
     * @return
     */
    public static <K, V> Map<K, V> json2Map(String json) {
        JSONObject jsonObject = JSONObject.fromObject(json);
        Map<K,V> map = new HashMap<K,V>();
          for(Iterator iter = jsonObject.keys(); iter.hasNext();){
              K key = (K)iter.next();
              map.put(key, (V) jsonObject.get(key));
          }
        return map;
    }




    public static void main(String args[]) {
        User user = new User("zhangsan", "boy", "20");
        try {
            //1.object 转换为json
            String userString = JSONObject.fromObject(user).toString();
            System.out.println("userString = " + userString);
            //2. json串转换为对象
            String json = new String("{\"age\":\"20\",\"name\":\"zhangsan\",\"sex\":\"boy\"}");
            User user1 = JsonUtil.jsonToObject(json, User.class);
            System.out.println("user1.toString = " + user1.toString());
            //3. list转换为string
            List<User> lst = new ArrayList<User>();
            lst.add(user);
            lst.add(user1);
            JSONArray jsonArray = JSONArray.fromObject(lst);
            System.out.println("jsonList1 = " + jsonArray.toString());
            String jsonList2 = JsonUtil.collection2Json(lst);
            System.out.println("jsonList2 = " + jsonList2);
            //4. string转换为list
            JSONArray jsonArray1 = JSONArray.fromObject(jsonList2);
            List<User> userList = (List<User>) JSONArray.toCollection(jsonArray1, User.class);
            System.out.println(("userList = ") + Joiner.on(",").join(userList));
              //5. map转为String
            ImmutableMap<String, Integer> mapImmutableInt = ImmutableMap.of("1", 1, "2", 2, "3", 3);
              Map<String,Integer> map=mapImmutableInt;
            String jsonMap =  JSONObject.fromObject(map).toString();
            System.out.println("jsonMap = "+ jsonMap);
            //6. String 转map
            JSONObject jsonObject = JSONObject.fromObject(jsonMap);
            Map<String,Integer> map1=jsonObject;
            System.out.println("jsonObject = " + map1.toString());


            Map<String,Integer> map2 = JsonUtil.json2Map(jsonMap);
            System.out.println("map2 = " + map2.toString());
            //7. array 转json
            int[] ints = {10, 9, 30, 8, 1};
            JSONArray jsonArray2 = JSONArray.fromObject(ints);
            System.out.println("jsonArray2 = " + jsonArray2);
            //8.json转array
            Object jsonArray3 = JSONArray.toArray(jsonArray2);
            System.out.println("jsonArray3 = " + jsonArray3);
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值