当前位置:网站首页>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);
}
}
边栏推荐
- MYSQL的下载与配置 mysql远程操控
- Using recursion to find all gray codes with n bits
- Summary of alter operation in SQL
- Binary search-1
- 字符串String转换为jsonArray并解析
- Crawl Douban to read top250 and import it into SqList database (or excel table)
- Clion编译catkin_ws(ROS工作空间包的简称)加载CMakeLists.txt出现的问题
- Decompilation of zero time technology smart contract security series articles
- Solidity - 合约继承子合约包含构造函数时报错 及 一个合约调用另一合约view函数收取gas费用
- Conditional compilation in precompiling instructions
猜你喜欢
随机推荐
SQL中的并、交、差运算
sql 中的alter操作总结
微信小程序 自定义 弹框组件
How about opening an account at Guojin securities? Is it safe to open an account?
The cross compilation environment appears So link file not found problem
System table SQLite of SQLite database_ master
转:苹果CEO库克:伟大的想法来自不断拒绝接受现状
JS common regular expressions
利用递归实现求n位所有格雷码
商品秒杀系统
GDB installation
In and exceptions, count (*) query optimization
Interview key points that must be mastered index and affairs (with B-tree and b+ tree)
Solidity - contract inheritance sub contract contains constructor errors and one contract calls the view function of another contract to charge gas fees
微信小程序 uniapp 左滑 删除 带删除icon
CD-CompactDisk
To: Apple CEO Cook: great ideas come from constantly rejecting the status quo
xlua获取ugui的button注册click事件
Enter n integers and output the number of occurrences greater than or equal to half the length of the array
sql中的几种删除操作








