• Object 转 json 工具类


    /**
    * 把数据对象转换成json字符串 DTO对象形如:{"id" : idValue, "name" : nameValue, ...}
    * 数组对象形如:[{}, {}, {}, ...] map对象形如:{key1 : {"id" : idValue, "name" :
    * nameValue, ...}, key2 : {}, ...}
    *
    * @param object
    * @return
    */
    public static String getJSONString(Object object) {
    String jsonString = null;
    // 属性值处理器
    JsonConfig jsonConfig = new JsonConfig();
    try{
    jsonConfig.registerJsonValueProcessor(Date.class,new JsonDateValueProcessor());
    //整形转换为字符串
    jsonConfig.registerJsonValueProcessor(Integer.class, new IntegerValueProcessor());
    if (object != null) {
    if (object instanceof Collection || object instanceof Object[]) {
    jsonString = JSONArray.fromObject(object, jsonConfig)
    .toString();
    } else {
    jsonString = JSONObject.fromObject(object, jsonConfig)
    .toString();
    }
    }
    } catch(Exception ex){
    ex.printStackTrace();
    }
    return jsonString == null ? "{}" : jsonString;
    }

  • 相关阅读:
    OPC UA认识汇总
    linux内核铁三角-进程(任务)调度
    nginx配置文件
    git和coding的使用
    php处理mysql的结果集
    php中self和$this还有parent的区别
    Mysql练习题
    SEO优化
    css3 旋转 八仙桌
    laravel笔记
  • 原文地址:https://www.cnblogs.com/Struts-pring/p/5290744.html
Copyright © 2020-2023  润新知