• 字符串和json数据的转换


    字符串和json数据转换

    json字符串数据转成bean对象

    package cn.csl.common.utils;
    
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.serializer.SerializeConfig;
    import com.alibaba.fastjson.serializer.SerializeFilter;
    import com.alibaba.fastjson.serializer.SerializerFeature;
    import lombok.extern.slf4j.Slf4j;
    
    @Slf4j
    public class JsonUtils {
    
      private JsonUtils() {}
    
      public static String toString(Object obj) {
        return JSON.toJSONString(obj);
      }
    
      public static String toString(Object object, SerializeFilter filter, SerializerFeature... features) {
        return JSON.toJSONString(object, SerializeConfig.globalInstance, new SerializeFilter[] {filter}, null, JSON.DEFAULT_GENERATE_FEATURE, features);
      }
    
      public static Object toObject(String str) {
        return JSON.parse(str);
      }
    
      @SuppressWarnings("unchecked")
      public static Map<String, Object> toMap(String str) {
        try {
          return (Map<String, Object>) JSON.parse(str);
        } catch (Exception e) {
          log.error(e.getMessage(), e);
          return new HashMap<String, Object>();
        }
      }
    
      @SuppressWarnings("unchecked")
      public static Map<String, Object> tryToMap(String str) {
        try {
          return (Map<String, Object>) JSON.parse(str);
        } catch (Exception e) {
          return new HashMap<String, Object>();
        }
      }
    
      @SuppressWarnings("unchecked")
      public static List<Map<String, Object>> toList(String str) {
    
        return (List<Map<String, Object>>) JSON.parse(str);
      }
    
      public static <T> T toBean(String str, Class<T> cls) {
        return JSON.parseObject(str, cls);
      }
    
      public static <T> List<T> toBeanList(String str, Class<T> cls) {
    
        return JSON.parseArray(str, cls);
      }
    }
  • 相关阅读:
    c# 图像转化成灰度图
    文件操作 流
    GBK UTF8 GB2312 流
    助力奥巴马,拯救大气层
    ASP.NET 缓存技术
    GridView 和 ViewState 来实现条件查寻
    把日期按指定格式输出
    创业灵感淘宝网
    文件_上传_下载
    java23种设计模式与追MM
  • 原文地址:https://www.cnblogs.com/luoyeyue/p/10640717.html
Copyright © 2020-2023  润新知