zoukankan      html  css  js  c++  java
  • json方法

    http://penghuaiyi.iteye.com/blog/1922632

    package com.yd.web.util;
    import java.lang.reflect.Type;
    import java.util.HashMap;
    import java.util.Map;
    
    import com.google.gson.Gson;
    /**
     * Java对象和JSON字符串相互转化工具类
     * @author penghuaiyi
     * @date 2013-08-10
     */
    public final class JsonUtil {
    
        private JsonUtil(){}
    
        /**
         * 对象转换成json字符串
         * @param obj
         * @return
         */
        public static String toJson(Object obj) {
            Gson gson = new Gson();
            return gson.toJson(obj);
        }
        /**
         * 将Map转化为Json
         *
         * @param map
         * @return String
         */
        public static <T> String mapToJson(Map<String, T> map) {
            Gson gson = new Gson();
            String jsonStr = gson.toJson(map);
            return jsonStr;
        }
        /**
         * json字符串转成对象
         * @param str
         * @param type
         * @return
         */
        public static <T> T fromJson(String str, Type type) {
            Gson gson = new Gson();
            return gson.fromJson(str, type);
        }
    
        /**
         * json字符串转成对象
         * @param str
         * @param type
         * @return
         */
        public static <T> T fromJson(String str, Class<T> type) {
            Gson gson = new Gson();
            return gson.fromJson(str, type);
        }
    
        public static void main(String[] args) {
            Map<String,Object> ma = new HashMap<String, Object>();
           ma.put("good","good");
           ma.put("time",null);
           String json =  mapToJson(ma);
            System.out.println(json);
    
        }
    
    }
    

      

  • 相关阅读:
    移动端hybrid开发复盘
    node/webpack 调试 loader 等技巧
    javascript(js)小数精度丢失的解决方案
    必经之路--买房之后需要走的流程--针对 组合贷款方式
    canvas 画半圆的两种方式
    svg path 画圆
    1.快速排序
    7.桥接设计模式
    6.适配器设计模式
    5.策略设计模式
  • 原文地址:https://www.cnblogs.com/sj521/p/6155009.html
Copyright © 2011-2022 走看看