lombok @toString转化json

本文介绍如何使用Lombok将带有逗号和括号的字符串转换为JSON格式,通过`toStringDataToJson`类展示了解决字符串中','和'('、')'的问题,适用于Java对象转JSON的需求。
摘要由CSDN通过智能技术生成
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));
	    }
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值