当前位置:网站首页>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);
}
}
边栏推荐
- Which securities company is better for a novice to open a stock trading account? How is it safer to speculate in stocks??
- Solidity - 合约继承子合约包含构造函数时报错 及 一个合约调用另一合约view函数收取gas费用
- JVM entry door (1)
- Summary of knowledge points
- Handwritten promise all
- 你了解如何比较两个对象吗
- Reading notes: process consulting III
- Tag dynamic programming - preliminary knowledge for question brushing -2 0-1 knapsack theory foundation and two-dimensional array solution template
- CD-CompactDisk
- 50 lines of code to crawl TOP500 books and import TXT documents
猜你喜欢
随机推荐
in和exsits、count(*)查询优化
深度学习之Numpy篇
9. Intelligent transportation project (2)
System table SQLite of SQLite database_ master
Tag dynamic programming - preliminary knowledge for question brushing -2 0-1 knapsack theory foundation and two-dimensional array solution template
Which securities company is better for a novice to open a stock trading account? How is it safer to speculate in stocks??
比较两个对象的大小关系原来可以如此花里胡哨
ARM裸板调试之串口打印及栈初步分析
VCD-影音光碟
Ethereum技术架构介绍
Union, intersection and difference operations in SQL
Publish message publishers and subscribe message subscribers of ROS
Introduction to Ethereum Technology Architecture
Deep understanding of MySQL lock and transaction isolation level
Yujun product methodology
交叉编译环境出现.so链接文件找不到问题
微服务架构
How about opening an account at Guojin securities? Is it safe to open an account?
Interview key points that must be mastered index and affairs (with B-tree and b+ tree)
Deep learning: numpy









