首页 > 解决方案 > 如何在下面的 json 文件中获取名称值

问题描述

[
  {
    "_class": "hudson.model.FreeStyleProject",
    "name": "actual_build",
    "url": "http://tepl.tallycloudinfra.com:8080/job/actual_build/",
    "color": "blue",
    "fullname": "actual_build"
  },
  {
    "_class": "hudson.model.FreeStyleProject",
    "name": "CIS_TEPL_QA_Deploy",
    "url": "http://tepl.tallycloudinfra.com:8080/job/CIS_TEPL_QA_Deploy/",
    "color": "blue",
    "fullname": "CIS_TEPL_QA_Deploy"
  },
  {
    "_class": "hudson.model.FreeStyleProject",
    "name": "cktest",
    "url": "http://tepl.tallycloudinfra.com:8080/job/cktest/",
    "color": "red",
    "fullname": "cktest"
  },
  {
    "_class": "hudson.model.FreeStyleProject",
    "name": "Create_New_DB_Structure",
    "url": "http://tepl.tallycloudinfra.com:8080/job/Create_New_DB_Structure/",
    "color": "blue",
    "fullname": "Create_New_DB_Structure"
  }
]

我在 jq 命令下尝试了这个错误。

错误:

jq -r '._class[].name' f1.json
jq: error (at f1.json:30): Cannot index array with string "_class"

jq -r '._class.name' f1.json
jq: error (at f1.json:30): Cannot index array with string "_class"

标签: jsongrepjq

解决方案


有了您的意见,

jq '.[].name' f1.json

产量

"actual_build"
"CIS_TEPL_QA_Deploy"
"cktest"
"Create_New_DB_Structure"

推荐阅读