JSONObject jsonObject = new JSONObject();
jsonObject.put("error_msg","success");
jsonObject.put("error_code",11);
// 设置下划线转驼峰
ParserConfig.getGlobalInstance().propertyNamingStrategy = PropertyNamingStrategy.SnakeCase;
Result result = JSON.parseObject(String.valueOf(jsonObject), Result.class);
System.out.println(result);
// 序列化配置
SerializeConfig serializeConfig = new SerializeConfig();
// 配值驼峰转下划线
serializeConfig.propertyNamingStrategy = PropertyNamingStrategy.SnakeCase;
String s = JSON.toJSONString(result,serializeConfig);
System.out.println(s);
结果 :
( errorMsg=success, errorCode=11)
{"error_code":11,"error_msg":"success"}