英文:
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
"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)
{
    "ACTION": [{
            "DONE ON": "",
            "DUE DATE": "15/03/23",
            "NOTE": "See document X",
            "TITLE": "Task 4"
        }
         
        ...
    ]
}
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.
import com.atlassian.jira.component.ComponentAccessor
import java.io.IOException
import com.atlassian.jira.issue.Issue
import groovy.json.*
import org.apache.log4j.Level
import java.util.regex.Matcher;
import java.util.regex.Pattern;
log.setLevel(Level.INFO);
def issue = issueManager.getIssueObject('PTIJ-68')
def description_raw = issue.getDescription()
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
"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)
{
    "ACTION": [{
            "DONE ON": "",
            "DUE DATE": "15/03/23",
            "NOTE": "See document X",
            "TITLE": "Task 4"
        }
         
        ...
    ]
}
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.
import com.atlassian.jira.component.ComponentAccessor
import java.io.IOException
import com.atlassian.jira.issue.Issue
import groovy.json.*
import org.apache.log4j.Level
import java.util.regex.Matcher;
import java.util.regex.Pattern;
log.setLevel(Level.INFO);
def issue = issueManager.getIssueObject('PTIJ-68')
def description_raw = issue.getDescription()
log.info("issue.getDescription() = ${issue.getDescription().toString()}")
Thanks.
答案1
得分: 1
以下是您要翻译的代码部分的中文翻译:
import groovy.json.JsonOutput
# 定义描述字符串
description = "|| ||到期日||完成于||OK/NOK||备注||\r\n|*任务1*| | | | |\r\n|*任务2*| | | | |\r\n|*任务3*| | | | |\r\n|*任务4*| 15/03/23| | | 查看文档X|"
# 初始化数据列表
data = []
# 使用正则表达式拆分描述字符串
description.splitEachLine( /\*?\|+\*?/ ){ _, title, dueDate, doneOn, ok_nok, remarks ->
  data << [ '标题': title, '到期日': dueDate.trim(), '完成于': doneOn.trim(), '备注': remarks.trim() ]
}
# 移除头部数据
data.remove 0
# 打印JSON格式的数据
JsonOutput.prettyPrint JsonOutput.toJson( ACTION:data )
打印结果:
{
    "ACTION": [
        {
            "标题": "任务1",
            "到期日": "",
            "完成于": "",
            "备注": ""
        },
        {
            "标题": "任务2",
            "到期日": "",
            "完成于": "",
            "备注": ""
        },
        {
            "标题": "任务3",
            "到期日": "",
            "完成于": "",
            "备注": ""
        },
        {
            "标题": "任务4",
            "到期日": "15/03/23",
            "完成于": "",
            "备注": "查看文档X"
        }
    ]
}
英文:
Some simplistic regex with CSV handling:
import groovy.json.JsonOutput
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|" 
List data = []
description.splitEachLine( /\*?\|+\*?/ ){ _, title, dueDate, doneOn, ok_nok, remarks ->
  data << [ 'TTILE':title, "DUE DATE":dueDate.trim(), "DONE ON":doneOn.trim(), "NOTE":remarks.trim() ]
}
data.remove 0 // skip the head
JsonOutput.prettyPrint JsonOutput.toJson( ACTION:data )
prints:
{
    "ACTION": [
        {
            "TTILE": "Task 1",
            "DUE DATE": "",
            "DONE ON": "",
            "NOTE": ""
        },
        {
            "TTILE": "Task 2",
            "DUE DATE": "",
            "DONE ON": "",
            "NOTE": ""
        },
        {
            "TTILE": "Task 3",
            "DUE DATE": "",
            "DONE ON": "",
            "NOTE": ""
        },
        {
            "TTILE": "Task 4",
            "DUE DATE": "15/03/23",
            "DONE ON": "",
            "NOTE": "See document X"
        }
    ]
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论