当前位置:网站首页>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);
}
}
边栏推荐
- sql 中的alter操作总结
- JSONUtils工具类(基于alibaba fastjson)
- Regular match same character
- 深度学习之Numpy篇
- tag动态规划-刷题预备知识-2. 0-1背包理论基础和二维数组解法模板
- 8VC Venture Cup 2017 - Final Round C. Nikita and stack
- redis 基础知识
- Request method 'POST' not supported
- Yujun product methodology
- Record of user behavior log in SSO microservice Engineering
猜你喜欢
随机推荐
图像二值化处理
Introduction to Ethereum Technology Architecture
Filebeat安装及使用
JVM entry door (1)
Handwritten numeral recognition based on tensorflow
一些基本错误
9. Intelligent transportation project (2)
Numpy之matplotlib
gdb安装
Current limiting design and Implementation
物联网协议的王者:MQTT
Create a time blocker yourself
wm_ Concat() and group_ Concat() function
Determine whether a sequence is a stack pop-up sequence
in和exsits、count(*)查询优化
链游开发成品源码 链游系统开发详情说明
IDEA收藏代码、快速打开favorites收藏窗口
Leetcode interview question 29 clockwise print matrix
临时关闭MySQL缓存
微服务架构