我有json和所有的密钥都在骆驼箱里,里面列出了对象。见下面我的json。我所有的财产在骆驼的案子里。场景就像我得到了camel case json对象,并将这个对象传递给第三方API。那个API正在接受带有较低外壳的蛇。
示例: knownLanguage to known_language passportNumber to passport_number
{
"knownLanguage": [
{
"language": "Marathi",
"isWrite": true,
"isRead": true,
"isSpeak": true
},
{
"language": "English",
"isWrite": true,
"isRead": true,
"isSpeak": true
}
],
"internationalInterest": {
"passportNumber": "1234567898",
"issueDate": "2020-06-01",
"expiryDate": "2030-06-01"
},
"educationDetails": [
{
"education": {
"degree": "Secondary",
"boardName": "UP",
"instituteName": "DAV",
"startMonth": "June",
"startYear": "2000",
"endMonth": "May",
"endYear": "2001",
"specialization": "",
"rollNumber": "",
"marksType": "CGPA",
"marks": "",
"country": "",
"state": "",
"district": ""
}
},
{
"education": {
"degree": "Bachelors",
"boardName": "UP",
"instituteName": "DAV",
"startMonth": "June",
"startYear": "2000",
"endMonth": "May",
"endYear": "2001",
"specialization": "",
"rollNumber": "",
"marksType": "CGPA",
"marks": "",
"country": "",
"state": "",
"district": ""
}
}
],
"employmentDetails": [
{
"employment": {
"employerName": "No",
"employmentType": "Permanent",
"isCurrent": true,
"jobType": "Full Time",
"startDate": "2022-06-14",
"endDate": "2022-06-15",
"jobRole": "Software Engineer",
"jobDescription": "",
"lastDrawnSalary": 10000,
"country": "India",
"state": "",
"district": "No"
}
}
],
"disabilityDetails": [
{
"disability": {
"disabilityType": "Blindness",
"disabilityPercentage": "20"
}
}
]
}我想用小写将上述json的所有递归键转换为下划线。
输出
{
"known_language": [
{
"language": "Hindi",
"is_write": true,
"is_read": true,
"is_speak": true
},
{
"language": "English",
"is_write": true,
"is_read": true,
"is_speak": true
}
],
"international_interest": {
"passport_number": "1234567898",
"issue_date": "2020-06-01",
"expiry_date": "2030-06-01"
},
"education_details": [
{
"education": {
"degree": "Secondary",
"board_name": "UP",
"institute_name": "DAV",
"start_month": "June",
"start_year": "2000",
"end_month": "May",
"end_year": "2001",
"specialization": "",
"roll_number": "",
"marks_type": "CGPA",
"marks": "",
"country": "",
"state": "",
"district": ""
}
},
{
"education": {
"degree": "Bachelors",
"board_name": "AU",
"institute_name": "AU",
"start_month": "June",
"start_year": "2003",
"end_month": "May",
"end_year": "2006",
"specialization": "Computer",
"roll_number": "",
"marks_type": "CGPA",
"marks": "",
"country": "",
"state": "",
"district": ""
}
}
],
"employment_details": [
{
"employment": {
"employer_name": "No",
"employment_type": "Permanent",
"is_current": true,
"job_type": "Full Time",
"start_date": "2022-06-14",
"end_date": "2022-06-15",
"job_role": "Software Engineer",
"job_description": "",
"last_drawn_salary": 10000,
"country": "India",
"state": "",
"district": "No"
}
}
],
"disability_details": [
{
"disability": {
"disability_type": "Blindness",
"disability_percentage": "20"
}
}
]
}有人知道怎么做吗?
发布于 2022-06-15 12:11:33
在java 9中,您可以尝试使用以下方法:
String newJsonString = Pattern.compile("[A-Z](?=(\\w*)\":)")
.matcher(jsonString)
.replaceAll(matchResult -> "_" + matchResult.group().toLowerCase());对于较低的版本,您可以尝试如下:
Pattern p = Pattern.compile("[A-Z](?=\\w*\":)");
Matcher m = p.matcher(jsonString);
StringBuffer sb = new StringBuffer();
while(m.find()){
String match = m.group();
m.appendReplacement(sb, "_" + match.toLowerCase());
}
m.appendTail(sb);
String newJsonString = sb.toString();添加:若要再次将其从蛇箱转回骆驼箱,您可以使用相同的代码,但请使用
Pattern p = Pattern.compile("_[a-z](?=\\w*\":)");和
m.appendReplacement(sb, match.substring(1).toUpperCase());相反,
发布于 2022-06-15 11:39:52
您可以使用Apache中的splitByCharacterTypeCamelCase;
这将返回拆分数组,使用()连接它们;
private static String getSnakeCaseString(String camelCaseString) {
String[] split = StringUtils.splitByCharacterTypeCamelCase(camelCaseString);
String joinedString = StringUtils.join(split, "_");
return joinedString.toLowerCase();
}输入“stringToChange”的示例输出:
string_to_change对于要更改的所有键,请使用此方法。
编辑::
String jsonString = "{\n" +
" \"degree\": \"Secondary\",\n" +
" \"boardName\": \"UP\",\n" +
" \"instituteName\": \"DAV\",\n" +
" \"startMonth\": \"June\",\n" +
" \"startYear\": \"2000\",\n" +
" \"endMonth\": \"May\",\n" +
" \"endYear\": \"2001\",\n" +
" \"specialization\": \"\",\n" +
" \"rollNumber\": \"\",\n" +
" \"marksType\": \"CGPA\",\n" +
" \"marks\": \"\",\n" +
" \"country\": \"\",\n" +
" \"state\": \"\",\n" +
" \"district\": \"\"\n" +
"}";
JSONObject jsonObject = new JSONObject(jsonString);
JSONObject newJSONObject = new JSONObject();
for (String key : jsonObject.keySet()) {
String newKey = getSnakeCaseString(key);
newJSONObject.put(newKey, jsonObject.get(key));
}
System.out.println(newJSONObject);这是一个更改键的示例代码。处理递归以更改所有键。
发布于 2022-06-22 11:34:45
番石榴的CaseFormat类可以将字符串转换为不同的格式。
String snakeCaseString = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, camelCaseString);至于递归迭代键,这取决于您是如何建模您的对象(一个定义良好的类?地图?杰克逊JsonNode?)
https://stackoverflow.com/questions/72630336
复制相似问题