欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

“{“key_one“:1,“key_two“:2}“转成小驼峰命名

程序员文章站 2023-12-23 12:33:40
...
package com.teamnavi.digitalCampus.utils;


import java.util.Optional;
import java.util.StringJoiner;

/**
 * 驼峰转换
 */
public class HumpConversionUtils {

/*    public static void main(String[] args) {
        String s = "{\"key_one\":\"1\",\"key_two\":\"2\"}";
        String conversion = conversion(s);
        System.out.println(conversion);
    }*/

    //"{"key_one":"1","key_two":"2"}"

    public static String conversion(String underLineStr){
        String[] split = underLineStr.split(",");
        StringJoiner sj = new StringJoiner(",");
        for (String s : split) {
            StringBuilder sb = new StringBuilder();
            String[] tmp = s.split(":");
            sb.append(tabCol2GetHump(tmp[0], ""));
            sb.append(":");
            sb.append(tmp[1]);
            sj.add(sb);
        }

        return sj.toString();
    }


    public static String tabCol2GetHump(String tableColumn,String prefix) {
        Optional<String> tableColumn1 = Optional.of(tableColumn.toLowerCase());
        String s = tableColumn1.get();
        String[] s1 = s.split("_");
        StringBuilder result = new StringBuilder(getPrefix(prefix));
        for (String value : s1) {
            result.append(first2UpperCase(value));
        }
        return result.toString();
    }


    /**
     * 首字母转大写
     * @param split
     * @return
     */
    public static String first2UpperCase(String split) {

        String substring = split.substring(0, 1);
        String upper = substring.toUpperCase();

        return upper + split.substring(1);
    }


    private static String getPrefix(String prefix) {
        Optional<String> prefix1 = Optional.ofNullable(prefix);
        return prefix1.orElse("");
    }
}

上一篇:

下一篇: