groovy 按键筛选列表给出空结果

qv7cva1a  于 2022-11-01  发布在  其他
关注(0)|答案(1)|浏览(138)

我尝试过滤一个列表,只显示包含"Total Votes"键的数据,但得到的结果是空的。下面是代码:

import org.apache.log4j.Logger;
import java.net.HttpURLConnection;
import org.json.JSONObject;
import org.apache.log4j.Level;

Logger log = Logger.getLogger("za.co.jira.groovy")

// Get the Change History for the issue key specified above
def issue = get("/rest/api/3/issue/${issue.key}/changelog")
        .header('Content-Type', 'application/json')
        .asObject(Map)
        .body

//Save the results as a map
def issueResult = issue.values.items as List<Map>

logger.info("Issue details:" + issueResult.findAll{it.field == 'Total Votes'

issueResult变量的输出为

[

[{
    "field": "status",
    "fieldtype": "jira",
    "fieldId": "status",
    "from": "3",
    "fromString": "In Progress",
    "to": "3",
    "toString": "In Progress"
}],
[{
    "field": "Total Votes",
    "fieldtype": "custom",
    "fieldId": "customfield_10067",
    "from": null,
    "fromString": "2",
    "to": null,
    "toString": "3"
}],
[{
    "field": "status",
    "fieldtype": "jira",
    "fieldId": "status",
    "from": "3",
    "fromString": "In Progress",
    "to": "3",
    "toString": "In Progress"
}],
[{
    "field": "Total Votes",
    "fieldtype": "custom",
    "fieldId": "customfield_10067",
    "from": null,
    "fromString": "3",
    "to": null,
    "toString": "2"
}],
[{
    "field": "status",
    "fieldtype": "jira",
    "fieldId": "status",
    "from": "3",
    "fromString": "In Progress",
    "to": "3",
    "toString": "In Progress"
}],
[{
    "field": "Total Votes",
    "fieldtype": "custom",
    "fieldId": "customfield_10067",
    "from": null,
    "fromString": "2",
    "to": null,
    "toString": "3"
}],
[{
    "field": "Total Votes",
    "fieldtype": "custom",
    "fieldId": "customfield_10067",
    "from": null,
    "fromString": "3",
    "to": null,
    "toString": "4"
}],
[{
    "field": "status",
    "fieldtype": "jira",
    "fieldId": "status",
    "from": "3",
    "fromString": "In Progress",
    "to": "3",
    "toString": "In Progress"
}],
[{
    "field": "Total Votes",
    "fieldtype": "custom",
    "fieldId": "customfield_10067",
    "from": null,
    "fromString": "4",
    "to": null,
    "toString": "3"
}]
[{
    "field": "Total Votes",
    "fieldtype": "custom",
    "fieldId": "customfield_10067",
    "from": null,
    "fromString": "3",
    "to": null,
    "toString": "4"
}],
[{
    "field": "status",
    "fieldtype": "jira",
    "fieldId": "status",
    "from": "3",
    "fromString": "In Progress",
    "to": "3",
    "toString": "In Progress"
}],
[{
    "field": "Total Votes",
    "fieldtype": "custom",
    "fieldId": "customfield_10067",
    "from": null,
    "fromString": "4",
    "to": null,
    "toString": "3"
}],
[{
    "field": "status",
    "fieldtype": "jira",
    "fieldId": "status",
    "from": "3",
    "fromString": "In Progress",
    "to": "3",
    "toString": "In Progress"
}],
[{
    "field": "Total Votes",
    "fieldtype": "custom",
    "fieldId": "customfield_10067",
    "from": null,
    "fromString": "3",
    "to": null,
    "toString": "4"
}],
[{
    "field": "status",
    "fieldtype": "jira",
    "fieldId": "status",
    "from": "3",
    "fromString": "In Progress",
    "to": "3",
    "toString": "In Progress"
}],
[{
    "field": "Total Votes",
    "fieldtype": "custom",
    "fieldId": "customfield_10067",
    "from": null,
    "fromString": "4",
    "to": null,
    "toString": "3"
}],

[{
    "field": "Total Votes",
    "fieldtype": "custom",
    "fieldId": "customfield_10067",
    "from": null,
    "fromString": "3",
    "to": null,
    "toString": "0"
}],
[{
    "field": "status",
    "fieldtype": "jira",
    "fieldId": "status",
    "from": "3",
    "fromString": "In Progress",
    "to": "3",
    "toString": "In Progress"
}],
[{
    "field": "Total Votes",
    "fieldtype": "custom",
    "fieldId": "customfield_10067",
    "from": null,
    "fromString": "0",
    "to": null,
    "toString": "1"
}],
[{
    "field": "status",
    "fieldtype": "jira",
    "fieldId": "status",
    "from": "3",
    "fromString": "In Progress",
    "to": "3",
    "toString": "In Progress"
}],
[{
    "field": "Total Votes",
    "fieldtype": "custom",
    "fieldId": "customfield_10067",
    "from": null,
    "fromString": "1",
    "to": null,
    "toString": "0"
}],
[{
    "field": "status",
    "fieldtype": "jira",
    "fieldId": "status",
    "from": "3",
    "fromString": "In Progress",
    "to": "3",
    "toString": "In Progress"
}],
[{
    "field": "Total Votes",
    "fieldtype": "custom",
    "fieldId": "customfield_10067",
    "from": null,
    "fromString": "0",
    "to": null,
    "toString": "1"
}],

[{
    "field": "status",
    "fieldtype": "jira",
    "fieldId": "status",
    "from": "3",
    "fromString": "In Progress",
    "to": "3",
    "toString": "In Progress"
}],
[{
    "field": "Total Votes",
    "fieldtype": "custom",
    "fieldId": "customfield_10067",
    "from": null,
    "fromString": "0",
    "to": null,
    "toString": "1"
}],
[{
    "field": "status",
    "fieldtype": "jira",
    "fieldId": "status",
    "from": "3",
    "fromString": "In Progress",
    "to": "3",
    "toString": "In Progress"
}],
[{
    "field": "Total Votes",
    "fieldtype": "custom",
    "fieldId": "customfield_10067",
    "from": null,
    "fromString": "1",
    "to": null,
    "toString": "0"
}],
[{
    "field": "status",
    "fieldtype": "jira",
    "fieldId": "status",
    "from": "3",
    "fromString": "In Progress",
    "to": "3",
    "toString": "In Progress"
}],

[{
    "field": "Total Votes",
    "fieldtype": "custom",
    "fieldId": "customfield_10067",
    "from": null,
    "fromString": "1",
    "to": null,
    "toString": "0"
}],
[{
    "field": "status",
    "fieldtype": "jira",
    "fieldId": "status",
    "from": "3",
    "fromString": "In Progress",
    "to": "10020",
    "toString": "Done"
}],

[{
    "field": "status",
    "fieldtype": "jira",
    "fieldId": "status",
    "from": "3",
    "fromString": "In Progress",
    "to": "3",
    "toString": "In Progress"
}],
[{
    "field": "Total Votes",
    "fieldtype": "custom",
    "fieldId": "customfield_10067",
    "from": null,
    "fromString": "0",
    "to": null,
    "toString": "1"
}],
[{
    "field": "status",
    "fieldtype": "jira",
    "fieldId": "status",
    "from": "3",
    "fromString": "In Progress",
    "to": "3",
    "toString": "In Progress"
}],

[{
    "field": "Voting Process Status",
    "fieldtype": "custom",
    "fieldId": "customfield_10073",
    "from": null,
    "fromString": null,
    "to": "10121",
    "toString": "Open"
}, {
    "field": "status",
    "fieldtype": "jira",
    "fieldId": "status",
    "from": "3",
    "fromString": "In Progress",
    "to": "3",
    "toString": "In Progress"
}],

[{
    "field": "Voting Process Status",
    "fieldtype": "custom",
    "fieldId": "customfield_10073",
    "from": "10120",
    "fromString": "Closed",
    "to": "10121",
    "toString": "Open"
}, {
    "field": "Operation",
    "fieldtype": "custom",
    "fieldId": "customfield_10074",
    "from": null,
    "fromString": null,
    "to": "10123",
    "toString": "Single"
}, {
    "field": "status",
    "fieldtype": "jira",
    "fieldId": "status",
    "from": "3",
    "fromString": "In Progress",
    "to": "3",
    "toString": "In Progress"
}]

]

但logger.info给出的结果为空[]

2lpgd968

2lpgd9681#

这个问题来自于项目的所有嵌套(列表中的1个对象在列表中的一个对象中)。我使用了groovy的JsonSlurper而不是org.json.JSONObject,类似这样的东西应该可以工作,最后你得到了一个所需的List<Map>

import groovy.json.JsonSlurper
// Mock of the API response
jsonText = """
{
  "values": [
    {
      "items": [
        {
            "field": "status"
        }
      ]
    },
    {
      "items": [
        {
            "field": "Total Votes",
            "fromString": "2",
            "toString": "3"
        }
      ]
    },
     {
      "items": [
        {
            "field": "Total Votes",
            "fromString": "3",
            "toString": "4"
        }
      ]
    }
  ]
}
"""

def jsonSlurper = new JsonSlurper()
def objectFromJson = jsonSlurper.parseText(jsonText)
// get ALL items but in a list not in a list of list
def items = objectFromJson.values.items.flatten()
def onlyTotalVotesItems = items.findAll {it.field == "Total Votes"}
println(onlyTotalVotesItems)

相关问题