我需要在 jenkins 管道中迭代一个 json 文件,以便获取“fields”值中的一些值,例如“System.AreaPath”,该 json 文件具有以下结构:
{
"fields": {
"AcornAgileProcess.BugSource": "Ad Hoc",
"AcornAgileProcess.IssueType": "Content",
"Custom.BuildVariant": "Default",
"Custom.ReproinRetail": "No",
"Custom.SQInvestigationStatus": "Not started",
"PhoenixAgileProcess.GameArea": "Art - technical/FX",
"PhoenixAgileProcess.GameMode": "In-Game (all)",
"PhoenixAgileProcess.ReproCount": 4,
"PhoenixAgileProcess.ReproRate": "5 - Consistently (100%)",
"PhoenixAgileProcess.ReproTime": 5,
"PhoenixAgileProcess.SkuTested": "sometext",
"System.AreaId": 1098,
"System.AreaLevel1": "sometext",
"System.AreaPath": "sometext",
"System.AssignedTo": {
"_links": {
"avatar": {
"href": "someurl"
}
},
"descriptor": "aad.NmE1MWI2NjMtOWU2Ny03NWYyLTlkOGEtNjMwMDFhMzVlZDI3",
"displayName": "Triage",
"id": "sometext",
"imageUrl": "someurl",
"uniqueName": "sometext",
"url": "someurl"
},
"System.AuthorizedAs": {
"_links": {
"avatar": {
"href": "someurl"
}
},
"descriptor": "aad.YmJkMGZjNmItMTBjYi03ODYxLTk0NTAtNGZjNGYwYjkzMmM3",
"displayName": "sometext",
"id": "someid",
"imageUrl": "someurl",
"uniqueName": "sometext",
"url": "someurl"
},
"System.AuthorizedDate": "2021-03-31T23:18:58.513Z",
"System.ChangedBy": {
"_links": {
"avatar": {
"href": "someurl"
}
},
"descriptor": "aad.YmJkMGZjNmItMTBjYi03ODYxLTk0NTAtNGZjNGYwYjkzMmM3",
"displayName": "sometext",
"id": "someid",
"imageUrl": "someurl",
"uniqueName": "sometext",
"url": "someurl"
},
"System.ChangedDate": "2021-03-31T23:18:58.513Z",
"System.CommentCount": 0,
"System.CreatedBy": {
"_links": {
"avatar": {
"href": "someurl"
}
},
"descriptor": "aad.NTcyMTBjZmQtY2VlOC03NjFhLTk4YzctYTc2YjA4NDUyZDU5",
"displayName": "sometext",
"id": "someid",
"imageUrl": "someurl",
"uniqueName": "sometext",
"url": "someurl"
},
"System.CreatedDate": "2021-03-31T23:16:45.42Z",
"System.Id": 896092,
"System.IterationId": 1093,
"System.IterationLevel1": "sometext",
"System.IterationPath": "sometext",
"System.NodeName": "sometext",
"System.PersonId": 31919392,
"System.Reason": "New",
"System.Rev": 2,
"System.RevisedDate": "9999-01-01T00:00:00Z",
"System.State": "New",
"System.TeamProject": "sometext",
"System.Title": "sometext",
"System.Watermark": 1589040,
"System.WorkItemType": "Bug"
},
"id": 896092,
"relations": [
{
"attributes": {
"isLocked": false,
"name": "Related"
},
"rel": "System.LinkTypes.Related",
"url": "someurl"
},
{
"attributes": {
"authorizedDate": "2021-03-31T23:16:45.42Z",
"id": 12326470,
"name": "ParticleEffects.mp4",
"resourceCreatedDate": "2021-03-31T23:09:55.437Z",
"resourceModifiedDate": "2021-03-31T20:03:35.273Z",
"resourceSize": 23883076,
"revisedDate": "9999-01-01T00:00:00Z"
},
"rel": "AttachedFile",
"url": "someurl"
}
],
"rev": 2,
"url": "someurl"
}
Run Code Online (Sandbox Code Playgroud)
我的常规知识非常基础,所以我尝试了以下两种方法:
def getTags(){
def json = readJSON file: "test.json"
def tags = ""
def slurped = new JsonSlurper().parseText(json)
slurped.each{ key, value ->
value.each {k, v ->
println "${v}"
}
}
}
Run Code Online (Sandbox Code Playgroud)
我得到这个错误15:30:31 hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: groovy.json.JsonSlurper.parseText() is applicable for argument types: (net.sf.json.JSONObject) values: [[fields:[AcornAgileProcess.BugSource:Ad Hoc, AcornAgileProcess.IssueType:Content, ...], ...]]
def getTags(){
def json = readJSON file: "test.json"
println json
def tags = ""
for (key in json.keySet()) {
if (key == "System.Tags"){
tags = json.get(key)
}
}
println tags
}
Run Code Online (Sandbox Code Playgroud)
对于这些,我不知道如何深入字段值以获取其中的值
def json = readJSON file: "test.json"
println json.fields.'System.AreaPath'
Run Code Online (Sandbox Code Playgroud)
它是一样的
def jsonString = readFile file: "test.json"
def json = new JsonSlurperClassic().parseText(jsonString)
println json.fields.'System.AreaPath'
Run Code Online (Sandbox Code Playgroud)
JsonSlurperClassic 返回可序列化的映射/数组 - 使用它来避免不可序列化的异常。
| 归档时间: |
|
| 查看次数: |
1271 次 |
| 最近记录: |