package com.skytech.justice; import cn.hutool.core.io.IoUtil; import cn.hutool.core.io.resource.ClassPathResource; import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import java.nio.charset.StandardCharsets; import java.util.Set; /** * @Author YY * @Date 2023/3/24 17:45 */ public class tets { public static void convertCamelCase(Object object) { if (object instanceof JSONArray) { JSONArray jsonarr = (JSONArray) object; for (Object obj : jsonarr) { convertCamelCase(obj); } } else if (object instanceof JSONObject) { JSONObject jsonobj = (JSONObject) object; Set<String> keys = jsonobj.keySet(); String[] array = keys.toArray(new String[keys.size()]); for (String key : array) { Object value = jsonobj.get(key); String ks = StrUtil.toCamelCase(key); jsonobj.remove(key); jsonobj.put(ks, value); convertCamelCase(value); } } } public static void convertUnderlineCase(Object object) { if (object instanceof JSONArray) { JSONArray jsonarr = (JSONArray) object; for (Object obj : jsonarr) { convertUnderlineCase(obj); } } else if (object instanceof JSONObject) { JSONObject jsonobj = (JSONObject) object; Set<String> keys = jsonobj.keySet(); String[] array = keys.toArray(new String[keys.size()]); for (String key : array) { Object value = jsonobj.get(key); String ks = StrUtil.toUnderlineCase(key); jsonobj.remove(key); jsonobj.put(ks, value); convertUnderlineCase(value); } } } public static void main(String[] args) { ClassPathResource resource = new ClassPathResource("temp.json"); String tempstr = IoUtil.read(resource.getStream(), StandardCharsets.UTF_8); JSONObject jsonobj = JSONObject.parseObject(tempstr); } }
递归下划线还驼峰
最新推荐文章于 2024-11-04 23:24:03 发布