将字段转驼峰,获取对象中的属性值

复制代码
// 获取对象中属性的值  
public String getFieldValue(String attrName)
        throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        // 转驼峰
        String standardField = getStandardField(attrName.toLowerCase());
        String getField = "get" + standardField;

        return this.getClass().getMethod(getField).invoke(this).toString();

    }

    // 获取驼峰字段
    private  String getStandardField(String field){
        int index = field.indexOf("_");
        if(index==-1){
            return field.substring(0,1).toUpperCase() + field.substring(1);
        }
        String prefix = field.substring(0, index);
        String upperCase = field.substring(index, index + 2).replace("_","").toUpperCase();
        String suffix = field.substring(index + 2 );
        return getStandardField(prefix + upperCase + suffix);

    }
复制代码

 

posted @   我没有出家  阅读(20)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
点击右上角即可分享
微信分享提示