zoukankan      html  css  js  c++  java
  • 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;
    }

  • 相关阅读:
    Django url
    Django 命令
    MVC和MTV模式
    pymysql操作
    mysql 基本操作
    jquery基本操作
    外边距内边距
    css
    Html
    __name__ __main__ 作用
  • 原文地址:https://www.cnblogs.com/Struts-pring/p/5290744.html
Copyright © 2011-2022 走看看