当前位置:网站首页>JSONUtils工具类(基于alibaba fastjson)
JSONUtils工具类(基于alibaba fastjson)
2022-06-26 18:25:00 【cc_南柯一梦】
1、引入maven依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.75</version>
</dependency>2、代码
package com.cc.common.utils;
import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import java.util.HashMap;
import java.util.Map;
/**
* json工具类
* @author cc
* @data 2021年06月30日 23:25
*/
public class JSONUtils {
/**
* Bean对象转JSON
* @param object
* @param dataFormatString
* @return
*/
public static String beanToJson(Object object, String dataFormatString) {
if (object != null) {
if (StringUtils.isEmpty(dataFormatString)) {
return JSONObject.toJSONString(object);
}
return JSON.toJSONStringWithDateFormat(object, dataFormatString);
} else {
return null;
}
}
/**
* Bean对象转JSON
* @param object
* @return
*/
public static String beanToJson(Object object) {
if (object != null) {
return JSON.toJSONString(object);
} else {
return null;
}
}
/**
* String转JSON字符串
* @param key
* @param value
* @return
*/
public static String stringToJsonByFastjson(String key, String value) {
if (StringUtils.isEmpty(key) || StringUtils.isEmpty(value)) {
return null;
}
Map<String, String> map = new HashMap<String, String>();
map.put(key, value);
return beanToJson(map, null);
}
/**
* 将json字符串转换成对象
* @param json
* @param clazz
* @return
*/
public static Object jsonToBean(String json, Object clazz) {
if (StringUtils.isEmpty(json) || clazz == null) {
return null;
}
return JSON.parseObject(json, clazz.getClass());
}
/**
* json字符串转map
* @param json
* @return
*/
@SuppressWarnings("unchecked")
public static Map<String, Object> jsonToMap(String json) {
if (StringUtils.isEmpty(json)) {
return null;
}
return JSON.parseObject(json, Map.class);
}
}
边栏推荐
- 一些基本错误
- wm_ Concat() and group_ Concat() function
- PC end records 515 ground sweeping robot /scan data
- 项目实战六:分布式事务-Seata
- Padding percentage operation
- 必须要掌握的面试重点——索引和事务(附讲B-树与B+树)
- Eigen库计算两个向量夹角
- Current limiting design and Implementation
- The eigen library calculates the angle between two vectors
- Numpy之matplotlib
猜你喜欢
随机推荐
零时科技 | 智能合约安全系列文章之反编译篇
爬取豆瓣读书Top250,导入sqlist数据库(或excel表格)中
NFTGameFi链游系统开发详解方案丨链游系统开发原理解析
判断某个序列是否为栈的弹出序列
DVD-数字通用光盘
Résumé des points de connaissance
The cross compilation environment appears So link file not found problem
带你解决哈希冲突,并实现一个简单hash表,
预编译处理指令中的条件编译
sqlite数据库的系统表sqlite_master
Leetcode 238 product of arrays other than itself
JVM入个门(1)
比较两个对象的大小关系原来可以如此花里胡哨
To: seek truth from facts
Leetcode 128 longest continuous sequence
Using recursion to find all gray codes with n bits
Numpy's Matplotlib
刻录光盘的程序步骤
Leetcode interview question 29 clockwise print matrix
LeetCode 128最长连续序列








