首页 > 其他分享 >fastjson修改json的时间转换,包含递归json子集的时间

fastjson修改json的时间转换,包含递归json子集的时间

时间:2023-03-05 18:34:45浏览次数:31  
标签:fastjson instanceof String JSONObject value json 子集 matcher public

JSONDateMatchersEnum时间格式枚举类
package convert;

public enum JSONDateMatchersEnum {

DEFAULT_MATCHER("yyyy-MM-dd HH:MM:ss", "^((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\\d):[0-5]?\\d:[0-5]?\\d$");

private final String format;
private final String matcher;

private JSONDateMatchersEnum(String format, String matcher) {
this.format = format;
this.matcher = matcher;
}

public String getFormat() {
return format;
}

public String getMatcher() {
return matcher;
}
}

包含递归json子集的时间值,修改时间值的转换工具类

package convert;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class JSONConvertUtils {

private JSONConvertUtils() {

}

public static String convertTimeZone(String jsonStr) {
JSONObject ret = convertTimeZone(JSONObject.parseObject(jsonStr, Feature.OrderedField));
return JSONObject.toJSONString(ret);
}

public static JSONObject convertTimeZone(JSONObject jsonObject) {
for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
Object value = entry.getValue();
if (value instanceof JSONArray) {
handleArray((JSONArray) value);
}
if (value instanceof JSONObject) {
handleJsonObject((JSONObject) value);
}
if (value instanceof String) {
String newValue = handleMatcherDateValue((String) value);
entry.setValue(newValue);
}
}
return jsonObject;
}

public static void handleArray(JSONArray array) {
for (int i = 0; i < array.size(); i++) {
Object object = array.get(i);
if (object instanceof JSONArray) {
handleArray((JSONArray) object);
}
if (object instanceof JSONObject) {
handleJsonObject((JSONObject) object);
}
if (object instanceof String) {
String newValue = handleMatcherDateValue((String) object);
array.set(i, newValue);
}
}
}

public static void handleJsonObject(JSONObject jsonObject) {
for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
Object value = entry.getValue();
if (value instanceof JSONArray) {
handleArray((JSONArray) value);
}
if (value instanceof JSONObject) {
handleJsonObject((JSONObject) value);
}
if (value instanceof String) {
String newValue = handleMatcherDateValue((String) value);
entry.setValue(newValue);
}
}
}

public static String handleMatcherDateValue(String value) {
for (JSONDateMatchersEnum enums : JSONDateMatchersEnum.values()) {
Pattern p = Pattern.compile(enums.getMatcher());
Matcher m = p.matcher(value);
if (m.matches()) {
//转换时区日期格式
//System.out.println(enums.getFormat() + " " + value);
return "2099-09-09 09:09:09";
}
}
return value;
}


// public static void main(String[] args) {
//
// String json = JSONObject.toJSONString(jsonStr, SerializerFeature.DisableCircularReferenceDetect);
// System.out.println("替换前的信息是 :\n" + json);
// System.out.println("---------------------------");
//
//
// String resultString = JSONConvertUtils.convertTimeZone(json);
// System.out.println("替换后的信息是 :\n" + resultString);
// System.out.println("---------------------------");
//
//
// Pattern pat = Pattern.compile("^((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\\d):[0-5]?\\d:[0-5]?\\d$");
// Matcher matcher = pat.matcher("2023-03-03 23:59:00");
// if (matcher.matches()) {
// System.out.println("时间格式-------:");
// }
//
// }
}


标签:fastjson,instanceof,String,JSONObject,value,json,子集,matcher,public
From: https://www.cnblogs.com/meitanzai/p/17181243.html

相关文章

  • json转golang结构
    在Golang中,我们可以使用标准库中的encoding/json包来将JSON数据转换为结构体。本文将介绍如何使用该包将JSON数据转换为Golang结构体。首先,让我们看一下JSON数据的格式。......
  • 浅析排列组合、斯特林数、贝尔数、二项式定理与推论及其反演、子集反演、广义容斥
    浅析排列组合、斯特林数、贝尔数、二项式定理与推论及其反演、子集反演、广义容斥目录浅析排列组合、斯特林数、贝尔数、二项式定理与推论及其反演、子集反演、广义容斥更......
  • 6.手写JSON.stringify、JSON.parse ?
    JSON.stringifyJSON.stringify() 方法将一个JavaScript对象或值转换为JSON字符串,如果指定了一个replacer函数,则可以选择性地替换值,或者指定的replacer是数组,则可......
  • 高效自动化工具之在线json
    在开发过程中,遇到很多问题,其中特别棘手的一个问题,就是一些批量工作,该类工作非常重要,但是又没有技术含量。比如:批量给一个变量加上前缀;批量找出xml里面的数字;根据数组,批量生......
  • golang标准库 json
    序列化:将内存中对象存储下来,把它变成一个个字节,转为二进制数据反序列化:将文件的一个个字节恢复成内存中队形。从二进制数据中恢复字符序列化:jsonxml二进制序列化:proto......
  • 动态规划(8)、416. 分割等和子集
    题目链接:416.分割等和子集-力扣(LeetCode) ......
  • 高性能 Jsonpath 框架,Snack3 3.2.57 发布
    Snack3,一个高性能的JsonPath框架借鉴了Javascript所有变量由var申明,及Xmldom一切都是Node的设计。其下一切数据都以ONode表示,ONode也即Onenode之意,代表任何......
  • FastJson JdbcRowSetImpl
    Java安全之FastJsonJdbcRowSetImpl链分析利用限制RMI利用的JDK版本≤JDK6u132、7u122、8u113LADP利用JDK版本≤6u211、7u201、8u191因为主要是FastJson,所以就不......
  • powershell脚本将json文件至SQL Server
    一、问题引入之前写过一篇博客关于T-SQL脚本将json文件至SQLServer,但T-SQL脚本只能在SSMS软件下运行。现在迫切需要一种能在操作系统shell中运行的脚本,在StackOverflow......
  • Error: Cannot find module ‘webpack-cli/package.json‘
    webpack安装后需要安装webpackcli:npminstall-dwebpack-cli1运行后报错:Error:Cannotfindmodule'webpack-cli/package.json'1解决方案:全局安装webpack-clinpmi......