我正在尝试将我的API的错误详细信息框架到Mail Body中。 在第一行中我想提到API,在下一行中我需要提供错误详细信息。但邮件正如下所示:
WEATHER_POST is Failing and the Error details are below.. { "FaultId": "Invalid
method type found in Request.", "fault": "FAULT_INVALID_METHOD_TYPE_IN_REQUEST"
}BILLS_API is Failing and the Error details are below..
{"error":"invalid_token","error_description":"Invalid access token: 158cd00a-
4942-4771-b54c-ed3da0f15a6c"}
但我希望看到如下信息
WEATHER_POST is Failing and the Error details are below..
{ "FaultId": "Invalid
method type found in Request.", "fault": "FAULT_INVALID_METHOD_TYPE_IN_REQUEST"
}
BILLS_API is Failing and the Error details are below..
{"error":"invalid_token","error_description":"Invalid access token: 158cd00a-
4942-4771-b54c-ed3da0f15a6c"}
下面是代码,我有bean列表,其中包含api和错误详细信息
StringBuffer message = new StringBuffer();
for (MessageDetails element : failureList) {
message.append(element.getApi()+" is Failing and the Error details are below.. ");
message.append(element.getBody());
}
JSONObject requestParams = new JSONObject();
requestParams.put("emailBody", message.toString());
任何人都可以帮忙吗?
答案 0 :(得分:2)
根据经验,我们可以尝试在输入字符串上进行正则表达式替换。每当看到开始或结束括号时,下面的选项会插入两个换行符。
input = input.replaceAll("(?<=\\}.)|(?=\\{)", "\n\n");
这是输出:
WEATHER_POST is Failing and the Error details are below..
{ "FaultId": "Invalid method type found in Request.", "fault": "FAULT_INVALID_METHOD_TYPE_IN_REQUEST}
BILLS_API is Failing and the Error details are below..
{"error":"invalid_token","error_description":"Invalid8 access token: 158cd00a- 4942-4771-b54c-ed3da0f15a6c]"}
当然,如果您可以在输入文本中使用嵌套括号,这将无法正常工作。但是,我们在您的示例数据中没有看到这一点。
修改强>
以上代码旨在成为您整条邮件的全面替代品,因此您可以修改您的确切代码:
String output = message.toString();
output = output.replaceAll("(?<=\\}.)|(?=\\{)", "\n\n");
JSONObject requestParams = new JSONObject();
requestParams.put("emailBody", output.toString());
答案 1 :(得分:1)
使用以下内容表示您需要换行。
buffer.append(System.getProperty("line.separator"));
答案 2 :(得分:1)
将新行"\n"
添加到您要附加到缓冲区的字符串中:
StringBuffer message = new StringBuffer();
for (MessageDetails element : failureList) {
message.append(element.getApi()+" is Failing and the Error details are below.. \n\n");
message.append(element.getBody()+"\n\n");
}
JSONObject requestParams = new JSONObject();
requestParams.put("emailBody", message.toString());