将多行字符串转换为单行字符串在Java中

huangapple go评论92阅读模式
英文:

Convert multiline string to single line in java

问题

以下是您要翻译的部分:

I want to convert a markdown table to a json string using Scriptrunner for Jira Server (Java/Groovy).

The table is stored in the description field of the issue and looks something like this when displaying the issue through the rest api /rest/api/2/issue

  1. "description": "|| ||Due Date||Done on||OK/NOK||Remarks||\r\n|*Task 1*| | | | |\r\n|*Task 2*| | | | |\r\n|*Task 3*| | | | |\r\n|*Task 4*| 15/03/23| | | See document X|"

For clarity, this is what the table looks like in interpreted MD:

Due Date Done on OK/NOK Remarks
Task 1
Task 2
Task 3
Task 4 15/03/23 See document X

This to be converted in the following Json (with a block for each line)

  1. {
  2. "ACTION": [{
  3. "DONE ON": "",
  4. "DUE DATE": "15/03/23",
  5. "NOTE": "See document X",
  6. "TITLE": "Task 4"
  7. }
  8. ...
  9. ]
  10. }

I have already figured out the regular expression to be used: https://regex101.com/r/Ph5f0a/1

Note that the table is always going to be the same, same number of lines and columns with this specific formatting.


Thing is when I display the content of the issue's description, the \r\n are interpreted as newlines.

Is there a way to display the content as a single line "raw" string so that my regex could work?

Here is the code snippet I used for now.

  1. import com.atlassian.jira.component.ComponentAccessor
  2. import java.io.IOException
  3. import com.atlassian.jira.issue.Issue
  4. import groovy.json.*
  5. import org.apache.log4j.Level
  6. import java.util.regex.Matcher;
  7. import java.util.regex.Pattern;
  8. log.setLevel(Level.INFO);
  9. def issue = issueManager.getIssueObject('PTIJ-68')
  10. def description_raw = issue.getDescription()
  11. log.info("issue.getDescription() = ${issue.getDescription().toString()}")
英文:

I want to convert a markdown table to a json string using Scriptrunner for Jira Server (Java/Groovy).

The table is stored in the description field of the issue and looks something like this when displaying the issue through the rest api /rest/api/2/issue

  1. "description": "|| ||Due Date||Done on||OK/NOK||Remarks||\r\n|*Task 1*| | | | |\r\n|*Task 2*| | | | |\r\n|*Task 3*| | | | |\r\n|*Task 4*| 15/03/23| | | See document X|"

For clarity, this is what the table looks like in interpreted MD:

  Due Date Done on OK/NOK Remarks
Task 1        
Task 2        
Task 3        
Task 4  15/03/23      See document X

This to be converted in the following Json (with a block for each line)

  1. {
  2. "ACTION": [{
  3. "DONE ON": "",
  4. "DUE DATE": "15/03/23",
  5. "NOTE": "See document X",
  6. "TITLE": "Task 4"
  7. }
  8. ...
  9. ]
  10. }

I have already figured out the regular expression to be used: https://regex101.com/r/Ph5f0a/1

Note that the table is always going to be the same, same number of lines and columns with this specific formatting.


Thing is when I display the content of the issue's description, the \r\n are interpreted as newlines.

Is there a way to display the content as a single line "raw" string so that my regex could work?

Here is the code snippet I used for now.

  1. import com.atlassian.jira.component.ComponentAccessor
  2. import java.io.IOException
  3. import com.atlassian.jira.issue.Issue
  4. import groovy.json.*
  5. import org.apache.log4j.Level
  6. import java.util.regex.Matcher;
  7. import java.util.regex.Pattern;
  8. log.setLevel(Level.INFO);
  9. def issue = issueManager.getIssueObject('PTIJ-68')
  10. def description_raw = issue.getDescription()
  11. log.info("issue.getDescription() = ${issue.getDescription().toString()}")

Thanks.

答案1

得分: 1

以下是您要翻译的代码部分的中文翻译:

  1. import groovy.json.JsonOutput
  2. # 定义描述字符串
  3. description = "|| ||到期日||完成于||OK/NOK||备注||\r\n|*任务1*| | | | |\r\n|*任务2*| | | | |\r\n|*任务3*| | | | |\r\n|*任务4*| 15/03/23| | | 查看文档X|"
  4. # 初始化数据列表
  5. data = []
  6. # 使用正则表达式拆分描述字符串
  7. description.splitEachLine( /\*?\|+\*?/ ){ _, title, dueDate, doneOn, ok_nok, remarks ->
  8. data << [ '标题': title, '到期日': dueDate.trim(), '完成于': doneOn.trim(), '备注': remarks.trim() ]
  9. }
  10. # 移除头部数据
  11. data.remove 0
  12. # 打印JSON格式的数据
  13. JsonOutput.prettyPrint JsonOutput.toJson( ACTION:data )

打印结果:

  1. {
  2. "ACTION": [
  3. {
  4. "标题": "任务1",
  5. "到期日": "",
  6. "完成于": "",
  7. "备注": ""
  8. },
  9. {
  10. "标题": "任务2",
  11. "到期日": "",
  12. "完成于": "",
  13. "备注": ""
  14. },
  15. {
  16. "标题": "任务3",
  17. "到期日": "",
  18. "完成于": "",
  19. "备注": ""
  20. },
  21. {
  22. "标题": "任务4",
  23. "到期日": "15/03/23",
  24. "完成于": "",
  25. "备注": "查看文档X"
  26. }
  27. ]
  28. }
英文:

Some simplistic regex with CSV handling:

  1. import groovy.json.JsonOutput
  2. String description = "|| ||Due Date||Done on||OK/NOK||Remarks||\r\n|*Task 1*| | | | |\r\n|*Task 2*| | | | |\r\n|*Task 3*| | | | |\r\n|*Task 4*| 15/03/23| | | See document X|"
  3. List data = []
  4. description.splitEachLine( /\*?\|+\*?/ ){ _, title, dueDate, doneOn, ok_nok, remarks ->
  5. data << [ 'TTILE':title, "DUE DATE":dueDate.trim(), "DONE ON":doneOn.trim(), "NOTE":remarks.trim() ]
  6. }
  7. data.remove 0 // skip the head
  8. JsonOutput.prettyPrint JsonOutput.toJson( ACTION:data )

prints:

  1. {
  2. "ACTION": [
  3. {
  4. "TTILE": "Task 1",
  5. "DUE DATE": "",
  6. "DONE ON": "",
  7. "NOTE": ""
  8. },
  9. {
  10. "TTILE": "Task 2",
  11. "DUE DATE": "",
  12. "DONE ON": "",
  13. "NOTE": ""
  14. },
  15. {
  16. "TTILE": "Task 3",
  17. "DUE DATE": "",
  18. "DONE ON": "",
  19. "NOTE": ""
  20. },
  21. {
  22. "TTILE": "Task 4",
  23. "DUE DATE": "15/03/23",
  24. "DONE ON": "",
  25. "NOTE": "See document X"
  26. }
  27. ]
  28. }

huangapple
  • 本文由 发表于 2023年3月3日 23:21:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75628907.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定