zoukankan      html  css  js  c++  java
  • 字符串和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);
      }
    }
  • 相关阅读:
    hadoop 2.0 详细配置教程
    大数据架构:flume-ng+Kafka+Storm+HDFS 实时系统组合
    KAFKA分布式消息系统
    element table组件懒加载
    vue将页面导出成pdf
    element upload上传前对文件专门bs64上传
    element table 通过selection-change选中的索引删除
    JavaScript实现Word、Excel、PPT在线预览
    数组对象位置对换
    textarea 根据光标位置添加内容
  • 原文地址:https://www.cnblogs.com/luoyeyue/p/10640717.html
Copyright © 2011-2022 走看看