对象有时候打印出来的日志可能是以map的格式存在,转成json格式
2024-09-08 19:22:12
package com.example.mybatiscodehelper.demo;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import java.io.*;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* @Author huangyanchang
* @Date 2022/9/8 22:29
*
* 用于将idNO=xxxx, userName=廖x文, cardNO=xxxxx, mobilePhone=xxxx, payChannelCode=ybp_1
* 成{"idNO":"xxxx","userName":"廖x文","cardNO":"xxxxx","mobilePhone":"xxxx","payChannelCode":"ybp_1"}
*
* @Version 1.0
*/
public class SpringConvertMap {
/**
* 用于将idNO=xxxx, userName=廖x文, cardNO=xxxxx, mobilePhone=xxxx, payChannelCode=ybp_1
* 转成{"idNO":"xxxx","userName":"廖x文","cardNO":"xxxxx","mobilePhone":"xxxx","payChannelCode":"ybp_1"}
* */
public static void main(String[] args) {
String str = "UserTest(id=1, name=hy)";
int i = str.indexOf("(");
char c = str.charAt(i);
String substring = str.substring(i + 1, str.lastIndexOf(")"));
System.out.println("subString: " + substring);
String lineTxt = readFile();
Map map = StringConvertMap(lineTxt);
System.out.println(new JSONObject(map));
System.out.println(substring);
}
private static String readFile() {
String lineTxt = "";
String str = "";
File file = new File("D:\\work\\demo\\src\\main\\resources\\log.txt");
if (file != null) {
try (InputStreamReader reader = new InputStreamReader(new FileInputStream(file), "utf-8")){
BufferedReader bufferedReader = new BufferedReader(reader);
if (file.isFile() && file.exists()) {
while ((lineTxt = bufferedReader.readLine()) != null) {
System.out.println("lineTex: " + lineTxt);
str = lineTxt;
}
} else {
System.out.println("读取文件出错!!");
}
} catch (IOException e) {
e.printStackTrace();
}
}
return str;
}
private static Map StringConvertMap(String toString) {
HashMap<Object, Object> map = new LinkedHashMap<>();
if (toString.contains("(") && toString.contains(")")) {
toString = toString.replace("(", "{");
toString = toString.replace(")", "}");
}
toString.trim();
String[] split = toString.split(",");
for (int i = 0; i < split.length; i++) {
String[] split1 = split[i].split("=");
map.put(split1[0], split1[1]);
}
return map;
}
}
最新文章
- java报表工具FineReport的公式编辑框的语法简介
- Linux上安装五笔
- H2 database 行相加-行列转换
- Delphi XE5 如何设计并使用FireMonkeyStyle(转)
- SQLite入门与分析(四)---Page Cache之事务处理(1)
- memcached 在windows下安装及启动
- iOS开发 - 不进入待机(屏幕保持唤醒)---UIApplication学习
- Keil伪指令
- Emmet 语法探析
- 一个简单的样例看明确怎样利用window.location.hash实现ajax操作时浏览器的前进/后退功能
- Python:Python学习总结
- Unity跨平台原理
- remoteViews简介
- 列举spark所有算子
- django-rest-framework-jwt
- java的引用
- JS笔记(二):对象
- web.net用户控件
- Nginx作为反向代理服务器
- Mysql中的auto_increment
热门文章
- STM32F4寄存器初始化:PWM输出
- element plus + vue3表单第一次数据未清空的bug问题解决
- 郁金香 中级班 2.c++的基类和派生类
- JZOJ 2474. 【GDKOI 2021普及组DAY2】我的世界
- (三) Mysql 之MVCC
- JavaScript之异步编程
- linux kali 换源细节
- RT-Thread Studio使用——创建工程并配置外部时钟(转)
- 四种语言刷算法之47. 全排列 II
- UVM——通过一个简单的testbench来了解UVM组件的phase执行顺序