簡體   English   中英

如何在 groovy 中迭代復雜的 json 結構?

[英]how to iterate a complex json structure in groovy?

我需要在我的 jenkins 管道中迭代 json 文件,以便在“字段”值中獲取一些值,例如“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"
}

我的 groovy 知識非常基礎,所以我嘗試了以下兩種方法:

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}"
    }
    }
}

with which I get this error 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
}

有了這些,我不知道如何更深入地了解字段值以獲取其中的值

我需要在我的 jenkins 管道中迭代 json 文件,以便在“字段”值中獲取一些值,例如“System.AreaPath”

你可以這樣做:

// get the JSON String from wherever you 
// are getting it, hardcoded here for simplicity
String 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"
}
'''

def jsonObject = new JsonSlurper().parseText(json)
def result = jsonObject.fields.'System.AreaPath'
println result

這將打印"sometext"

def json = readJSON file: "test.json"
println json.fields.'System.AreaPath'

def jsonString = readFile file: "test.json"
def json = new JsonSlurperClassic().parseText(jsonString)
println json.fields.'System.AreaPath'

JsonSlurperClassic 返回可序列化的映射/數組 - 使用它來避免不可序列化的異常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM