powershell拆分为哈希表

powershell拆分为哈希表,powershell,jira-rest-api,Powershell,Jira Rest Api,我正在尝试拆分从jira rest api获得的字符串,但我找不到一个好方法。 API返回此类对象 com.atlassian.greenhopper.service.sprint。Sprint@3b306c49[id=2792,rapidViewId=920,state=CLOSED,name=ABI 报告/支持sprint 12,startDate=2018-09-11T09:45:26.622+02:00,endDate=2018-09-27T22:00:00.000+02:00,Comp

我正在尝试拆分从jira rest api获得的字符串,但我找不到一个好方法。 API返回此类对象

com.atlassian.greenhopper.service.sprint。Sprint@3b306c49[id=2792,rapidViewId=920,state=CLOSED,name=ABI 报告/支持sprint 12,startDate=2018-09-11T09:45:26.622+02:00,endDate=2018-09-27T22:00:00.000+02:00,CompletedDate=2018-09-28T08:15:41.088+02:00,sequence=2792]com.atlassian.greenhopper.service.sprint。Sprint@c518022[id=2830,rapidViewId=920,state=ACTIVE,name=ABI 报告/支持sprint 13,startDate=2018-09-28T08:30:26.785+02:00,endDate=2018-10-16T20:30:00.000+02:00,CompletedDate=,sequence=2830]

我用它做的是

$sprints = $issue.fields.customfield_10012 | Select-String -Pattern '\x5b(.*)\x5d' | ForEach-Object {$_.Matches.Groups[1].Value}
其中$issue.fields.customfield_10012是从REST API返回的字段

这给了我exesse数据的对象条带,我可以使用这个

Foreach ($sprint in $sprints) {
  Try {
    #assign values to variable
    $sprint = $sprint -split ',' | Out-String
    $sprint = ConvertFrom-StringData -StringData $sprint
    [int]$sId = $sprint.id
    $sName = "N'" + $sprint.name.Replace("'", "''") + "'"
    #insert into sql using Invoke-Sqlcmd
  }
  Catch {
    #Write log msg into log table about error in Staging of the worklog for the ticket
    $logMsg = "Staging sprint ($sId) for ticket ($key): $($_.Exception.Message)"
    Write-Host $logMsg
  }
}
但我的用户很有创意,sprint的名字之一是sprint 11-AS、SS、RS,它打破了我的“拆分”和“转换为哈希表”

知道如何将这个字符串拆分成合适的哈希表吗

com.atlassian.greenhopper.service.sprint。Sprint@3b306c49[id=2792,rapidViewId=920,state=CLOSED,name=ABI 报告/支持sprint 12,startDate=2018-09-11T09:45:26.622+02:00,endDate=2018-09-27T22:00:00.000+02:00,CompletedDate=2018-09-28T08:15:41.088+02:00,sequence=2792]com.atlassian.greenhopper.service.sprint。Sprint@c518022[id=2830,rapidViewId=920,state=ACTIVE,name=Sprint 11 - AS,SS,RS,startDate=2018-09-28T08:30:26.785+02:00,endDate=2018-10-16T20:30:00.000+02:00,completeDate=,sequence=2830]


将字符串拆分为逗号,后跟等号的单词

在它们自己的行上处理这些记录,如果这与源数据不匹配,您仍然可以使用下面的逻辑,我们进行匹配,将大括号[]内的数据与大括号[]外的数据分开。然后,我们对上面讨论的内部数据进行拆分,并向前看,以获得哈希表

$lines = "com.atlassian.greenhopper.service.sprint.Sprint@3b306c49[id=2792,rapidViewId=920,state=CLOSED,name=ABI Reports/Support sprint 12,startDate=2018-09-11T09:45:26.622+02:00,endDate=2018-09-27T22:00:00.000+02:00,completeDate=2018-09-28T08:15:41.088+02:00,sequence=2792]", 
"com.atlassian.greenhopper.service.sprint.Sprint@c518022[id=2830,rapidViewId=920,state=ACTIVE,name=Sprint 11 - AS,SS,RS,startDate=2018-09-28T08:30:26.785+02:00,endDate=2018-10-16T20:30:00.000+02:00,completeDate=,sequence=2830]"


$lines | Where-Object{$_ -match "^(?<sprintid>.*)\[(?<details>.*)\]"} | ForEach-Object{
    $Matches.details -split ",(?=\w+=)" | Out-String | ConvertFrom-StringData
}


我有更多的经验与转换从StringData然而作为。。。也很强大,可以减少一些腿部工作。

您需要保留com.atlassian.greenhopper.service.sprint吗。Sprint@c518022? 该字符串文本中是否有两个不同的对象,并且在您的实际数据中可能是一个变量?您还想知道这些字符串是如何存在的吗?每行一张唱片?包含我们在编辑中看到的其他换行符?您是如何获取这些数据的?假设它来自rest api,那么它应该已经作为对象被检索了?@Matt根据他们访问返回的方式,它确实看起来已经是一个对象了。@TheIncorrigible1确实是这样。我应该停止这样做,同时停止我的实际工作。最后,一切都归结为正则表达式。这正是我所需要的。实际上我需要的是这个正则表达式,但我真的需要了解第一个正则表达式中发生了什么 id : 2792 startDate : 2018-09-11T09:45:26.622+02:00 completeDate : 2018-09-28T08:15:41.088+02:00 sequence : 2792 name : ABI Reports/Support sprint 12 rapidViewId : 920 endDate : 2018-09-27T22:00:00.000+02:00 state : CLOSED

id : 2830 startDate : 2018-09-28T08:30:26.785+02:00 completeDate : sequence : 2830 name : Sprint 11 - AS,SS,RS rapidViewId : 920 endDate : 2018-10-16T20:30:00.000+02:00 state : ACTIVE