1.不支持字符串按 , 号隔开的形式
import com.alibaba.csp.sentinel.util.StringUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.List;
/**
* lombok @toString转化json
*/
public class ToStringDataToJson {
private static <T> Object toJsonStringByLombok(String str, Class<T> classModel) {
if (StringUtil.isBlank(str)) {
return "Parameter Cannot Be Empty";
}
str = str.replace(")", "}");
while (str.indexOf("(") != -1) {
int i = str.indexOf("(");
int i1 = str.lastIndexOf("=", i);
String str1 = str.substring(0, i1 + 1);
String str4 = null;
str4 = str.substring(i1+1, i1 + 2);
String str2 = "{";
String str3 = str.substring(i + 1);
str = str4.equals("[") ? str1 + str4 + str2 + str3 : str1 + str2 + str3;
}
str = str.replace(" ", "")
.replace("=", "\"=\"")
.replace(",", "\",\"")
.replace("\"null\"", "null")
.replace("\"{", "{\"")
.replace("}\"", "}")
.replace("}", "\"}")
.replace("\"}\"}","\"}}")
.replace("\"[{","[{\"")
.replace("]\"","]");
str = "\"" + str.replace("=", ":");
if ("\"{".equals(str.substring(0,2))){
str = "{\"" +str.substring(2) ;
}else {
str = str.substring(str.indexOf(":") + 1);
}
str = str.replace("\"null\"", "null");
T json = JSONObject.parseObject(str,classModel);
return classModel == null ? str : json;
}
public static void main(String[] args) {
String str = "contactlessKmsAuth request=DesfireAuthRequest(hsm_group_name=auth, authentications=[DesfireAuthRequest.Authentication(key_index=6, key_value=null, div=88044a37012c1c80, div_method=rta, key_version=3, auth_data=null, random_data=EBBD345285C69FE6, random=FD8D8609B314DA96, desfire_decrypt_list=null, desfire_encrypt_list=null, defire_diversifyMDAC_list=null)])";
System.out.println(toJsonStringByLombok(str,null));
}
}
2.解决上述字符串,号情况
package com.snowball.kms.service.impl;
import com.alibaba.csp.sentinel.util.StringUtil;
import com.alibaba.fastjson.JSONObject;
import java.util.Arrays;
import java.util.List;
/**
* lombok toString转化json
*/
public class ToStringDataToJson {
private static <T> Object toJsonStringByLombok(String str, Class<T> classModel) {
if (StringUtil.isBlank(str)) {
return "Parameter Cannot Be Empty";
}
str = str.replace(")", "}");
while (str.indexOf("(") != -1) {
int i = str.indexOf("(");
int i1 = str.lastIndexOf("=", i);
String str1 = str.substring(0, i1 + 1);
String str4 = null;
str4 = str.substring(i1+1, i1 + 2);
String str2 = "{";
String str3 = str.substring(i + 1);
String str6 = "";
String str5 = str.substring(i1,i);
int i2 = str5.indexOf("},");
if (i2 > 0) {
str6 = str5.substring(1, i2 + 2);
}
str = str4.equals("[") ? str1 + str4 +str6 + str2 + str3 : str1 +str6+ str2 + str3;
}
boolean b = false;
if (!str.startsWith("{")) {
str = "{"+str+"}";
}
str = str.replace(" ", "")
.replace("=", "\"=\"")
.replace("\"{", "{\"")
.replace("}\"", "}")
.replace("}", "\"}")
.replace("\"}\"}","\"}}")
.replace("\"[{","[{\"")
.replace("]\"","]");
str = "\"" + str.replace("=", ":");
if ("\"{".equals(str.substring(0,2))){
str = "{\"" +str.substring(2) ;
}else {
str = str.substring(str.indexOf(":") + 1);
}
str = str.replace("\"null\"", "null");
List<String> list = Arrays.asList(str.split(","));
String s1 = list.get(0);
String s0 = "";
for (int i = 1; i < list.size(); i++) {
if (list.get(i).contains(":")) {
s0 += "\",\""+list.get(i);
}else {
s0 += ","+list.get(i);
}
}
String s = (s1 + s0).replace("\"[{","[{\"")
.replace("}]\"","\"}]")
.replace("}\"","\"}")
.replace("\"{","{\"")
.replace("\"\"}","\"}")
.replace("{\"\"","{\"")
;
T json = JSONObject.parseObject(s,classModel);
return classModel == null ? s : json;
}
public static void main(String[] args) {
String str = "contactlessKmsAuth request=DesfireAuthRequest(hsm_group_name=auth, authentications=[DesfireAuthRequest.Authentication(key_index=6, key_value=null, div=88044a37012c1c80, div_method=rta, key_version=3, auth_data=null, random_data=EBBD345285C69FE6, random=FD8D8609B314DA96, desfire_decrypt_list=null, desfire_encrypt_list=null, defire_diversifyMDAC_list=null)])";
System.out.println(toJsonStringByLombok(str,null));
}
}