gpt4 book ai didi

java - 丢失新行符号 (\r\n)

转载 作者:行者123 更新时间:2023-11-30 04:05:08 24 4
gpt4 key购买 nike

我有一个宏,它从 JIRA 获取整个问题信息(作为 JSON 对象)并将其打印在 Confluence 中。但在 Confluence 中,新行丢失,格式也乱了。

这是 Jira 中的文本(及其外观):

Reports are afterwards sent via ftp to Aviva Risk Management team.

Aviva risk team use the daily reports to extract some of the numbers and for a report on the risk performance of the portfolio.

这是它在 JSON 中的样子:

Reports are afterwards sent via ftp to Aviva Risk Management team.\r\n\r\nAviva risk team use the daily reports to extract some of the numbers and for a report on the risk performance of the portfolio.

这是之后打印的内容:

Reports are afterwards sent via ftp to Aviva Risk Management team. Aviva risk team use the daily reports to extract some of the numbers and for a report on the risk performance of the portfolio.

基本上,我从中得到的是 \n\r 符号丢失或者没有按照 Confluence 中应有的方式进行转换。我该如何解决这个问题?

编辑:@Octopus 这是代码。

public class FieldinfoMacro implements Macro {

@Override
@SuppressWarnings("unchecked")
// This macro gets the issue's field values from the page's context so you don't have to
// make new requests to Jira for each field seperately
public String execute(Map<String, String> par, String s, ConversionContext conversionContext)
throws MacroExecutionException {

// parameters
String add = (String) par.get("getfield").toLowerCase();
HashMap<String, String> fieldMap = (HashMap<String, String>) conversionContext.getProperty("fieldMap");
JSONObject issue = (JSONObject) conversionContext.getProperty("issueJson");
Object output = null;
Object result = null;
StringBuilder temp = new StringBuilder();

/* if (add.equals("reporter") || add.equals("assignee") || add.equals("account manager")) {
try {
output = issue.getJSONObject("fields").get(fieldMap.get(add));
result = ((JSONObject) output).get("emailAddress");
result = ((String) result).replaceAll("@.*", "");
result = ((String) result).replaceAll("\\W", " ");
} catch (JSONException je) {
je.printStackTrace();
}
} else { */
// get the requested field value from the issue
// there's a handler for the different types of returned data
// i.e. JSONObject, JSONArray, String
try {
output = issue.getJSONObject("fields").get(fieldMap.get(add));
// String handler
if (output instanceof String) {
result = output;
// JSONArray handler (covered different cases)
} else if (output instanceof JSONArray) {
for (int i = 0; i < ((JSONArray) output).length(); i++) {
if (((JSONArray) output).get(i) instanceof JSONObject) {
result = ((JSONArray) output).getJSONObject(i).getString("value");
} else {
if (((JSONArray) output).length() < 2) {
temp.append(((JSONArray) output).getString(i));
} else if (i < ((JSONArray) output).length() - 1) {
temp.append(((JSONArray) output).getString(i) + ", ");
} else temp.append(((JSONArray) output).getString(i));
result = temp;
}
}
// JSONObject handler
} else if (output instanceof JSONObject) {
result = ((JSONObject) output).getString("value");
}
} catch (JSONException je) {
je.printStackTrace();
}

return result.toString();
}

最佳答案

你不必担心这个。 JSON完全能够代表任何 UTF-8特点。收到 JSON 字符串后,您肯定会在代码中执行某些操作,从而消除 \r\n人物。您应该能够转换 \r\n到新行,例如添加 <br>通过编写你的逻辑来创建 html 标签。请在帖子中添加如何从 JSON 获取数据并将其打印在页面中

此行从字符串中删除所有“\r\n”

 result = ((String) result).replaceAll("\\W", " ");

这就是为什么字符串文字没有任何 \r\n人物。请删除该行并进行测试。请注意\W意思是“任何非单词字符”。即排除这些 [a-zA-Z0-9_] 的字符人物。希望这有帮助

关于java - 丢失新行符号 (\r\n),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20968942/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com