当前位置:网站首页>Jsonutils tool class (based on Alibaba fastjson)
Jsonutils tool class (based on Alibaba fastjson)
2022-06-26 18:33:00 【cc_ nanke dream】
1、 introduce maven rely on
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.75</version>
</dependency>2、 Code
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 Tool class
* @author cc
* @data 2021 year 06 month 30 Japan 23:25
*/
public class JSONUtils {
/**
* Bean Object turn 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 Object turn JSON
* @param object
* @return
*/
public static String beanToJson(Object object) {
if (object != null) {
return JSON.toJSONString(object);
} else {
return null;
}
}
/**
* String turn JSON character string
* @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);
}
/**
* take json Converts a string to an object
* @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 String rotation 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);
}
}
边栏推荐
- JS cast
- sql 中的alter操作总结
- ros::spinOnce()和ros::spin()的使用和区别
- (multi threading knowledge points that must be mastered) understand threads, create threads, common methods and properties of using threads, and the meaning of thread state and state transition
- Chinese (Simplified) language pack
- 分页查询、JOIN关联查询优化
- IK分词器
- PC端录制扫515地机器人/scan数据
- ISO documents
- GDB installation
猜你喜欢

Leetcode 238 product of arrays other than itself

Solidity - contract inheritance sub contract contains constructor errors and one contract calls the view function of another contract to charge gas fees

Redis single sign on system + voting system

ARM裸板调试之串口打印及栈初步分析

Filebeat安装及使用

博云,站在中国容器潮头

Introduction to Ethereum Technology Architecture

DVD digital universal disc

in和exsits、count(*)查询优化

手机影像内卷几时休?
随机推荐
知识点总结
To: seek truth from facts
Reading notes: process consulting III
8VC Venture Cup 2017 - Final Round C. Nikita and stack
Summary of alter operation in SQL
Tag dynamic programming - preliminary knowledge for question brushing -2 0-1 knapsack theory foundation and two-dimensional array solution template
JVM入个门(1)
利用递归实现求n位所有格雷码
Interview key points that must be mastered index and affairs (with B-tree and b+ tree)
LeetCode 238 除自身以外数组的乘积
Record of user behavior log in SSO microservice Engineering
Case study of row lock and isolation level
Comparing the size relationship between two objects turns out to be so fancy
Some basic mistakes
Redis Basics
交叉编译环境出现.so链接文件找不到问题
Which securities company is better for a novice to open a stock trading account? How is it safer to speculate in stocks??
商品秒杀系统
Clion编译catkin_ws(ROS工作空间包的简称)加载CMakeLists.txt出现的问题
SSO微服务工程中用户行为日志的记录