【问题标题】:Fetch value from json array key starts with @从 json 数组键中获取值以 @ 开头
【发布时间】:2019-02-13 06:29:22
【问题描述】:

我正在尝试访问

的值

@angular/核心

来自 json 文件如下所示

"dependencies": {
   "@angular/animations": "^6.1.0",
   "@angular/common": "^6.1.0",
   "@angular/compiler": "^6.1.0",
   "@angular/core": "^6.1.0",
   "@angular/forms": "^6.1.0",
   "@angular/http": "^6.1.0",
   "@angular/platform-browser": "^6.1.0",
   "@angular/platform-browser-dynamic": "^6.1.0",
   "@angular/router": "^6.1.0",
   "core-js": "^2.5.4",
   "rxjs": "~6.2.0",
   "zone.js": "~0.8.26"
},

我试过的代码是

    def packageString = readFile("app/package.json")  // read the json file
    def parsedPackageString =  new groovy.json.JsonSlurperClassic().parseText(packageString)

    echo "My Angular Project version $parsedPackageString.dependencies.@angular/core"

但它正在打印依赖项的所有值

我的预期结果是

"^6.1.0",

【问题讨论】:

    标签: groovy jenkins-groovy groovyshell groovy-console


    【解决方案1】:

    您只需为 parsedPackageString 指定属性:

    def parsedPackageString =  new groovy.json.JsonSlurperClassic().parseText(packageString).dependencies.'@angular/core'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-04
      • 1970-01-01
      相关资源
      最近更新 更多