Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用powershell获取json键值_Powershell - Fatal编程技术网

使用powershell获取json键值

使用powershell获取json键值,powershell,Powershell,我有以下json输出字符串: { "meta": { "limit": 20, "next": null, "offset": 0, "previous": null, "total_count": 1 }, "objects": [{ "bcontext": "/api/v2.0/buildercontext/2/", "bugs": [],

我有以下json输出字符串:

{
    "meta": {
        "limit": 20,
        "next": null,
        "offset": 0,
        "previous": null,
        "total_count": 1
    },
    "objects": [{
        "bcontext": "/api/v2.0/buildercontext/2/",
        "bugs": [],
        "build": {
            "bldtype": "obj",
            "branch": "main",
            "buildstatus": [{
                "build": "/api/v2.0/build/2140634/",
                "failurereason": "_checkfailures (seen: FAIL - /testrun/18647678/ - area[4769] AIM-SANITY)",
                "id": "1294397",
                "lastupdate": "2015-03-31T14:30:18",
                "overridden": false,
                "overridedesc": "",
                "overrideuser": null,
                "recommended": false,
                "resource_uri": "/api/v2.0/buildstatus/1294397/",
                "slatype": {
                    "id": "26",
                    "name": "VA_Bats",
                    "resource_uri": "/api/v2.0/sla/26/"
                }
            }],
            "changeset": "494625",
            "coverage": false,
            "deliverables": ["/api/v2.0/deliverable/4296455/", "/api/v2.0/deliverable/4296956/", "/api/v2.0/deliverable/4296959/", "/api/v2.0/deliverable/4296986/", "/api/v2.0/deliverable/4296992/", "/api/v2.0/deliverable/4296995/", "/api/v2.0/deliverable/4297034/", "/api/v2.0/deliverable/4297058/"],
            "git_host": null,
            "git_repo": null,
            "id": "2140634",
            "p4host": {
                "id": "10",
                "p4port": "perforce-rhino.eng.com:1800",
                "p4weburl": "http://p4web.eng.com:1800",
                "resource_uri": "/api/v2.0/perforceserver/10/"
            },
            "resource_uri": "/api/v2.0/build/2140634/",
            "site": "/api/v2.0/site/25/",
            "site_name": "mbu",
            "slastested": ["/api/v2.0/sla/26/"],
            "submit_time": "2015-03-31T05:40:21",
            "submit_user": "haharonof"
        },
        "builder": "/api/v2.0/builder/1423/",
        "clean": true,
        "componentbuilds": "vcops-vsphere-solution-pak=sb-5242047,vrops=sb-5242013,vscm=sb-5242025,vsutilities=sb-5242029;parentbuilder=1410",
        "deleted": false,
        "endtime": "2015-03-31T06:20:58",
        "helpzillas": [],
        "id": "4296956",
        "location": {
            "httpserver": "sc-prd-cat-services001.eng.com",
            "id": "1",
            "name": "PA",
            "nfsserver": "cat-results.eng.com",
            "pxedir": "/mts/builder-pxe",
            "resource_uri": "/api/v2.0/location/1/",
            "resultspath": "/results"
        },
        "nfsserver": "build-storage60",
        "p4client": "vmktestdevnanny-builder-1423",
        "path": "/storage60/release/sb-5242148",
        "ready": true,
        "resource_uri": "/api/v2.0/deliverable/4296956/",
        "result": "PASS",
        "sbbuildid": 5242148,
        "sbjobid": 5242148,
        "sbuser": "arajamanickam",
        "starttime": "2015-03-31T06:16:50",
        "targetchangeset": "494625",
        "targets": "vcopssuitevm",
        "triagetime": null,
        "vmodl": null
    }]
}

我想使用powershell获取
sbbuildid
。如何获取此信息?

通过使用将json转换为对象(假设
$jsonString
包含上述json):


将整个字符串放入
$build_info

这是一个非常脆弱的解决方案,它会捕获ID前面的空间,当您到达一个高于99999或低于1000000的构建ID时,它就会断开。我不确定为什么它会捕获空间。获取值后,我使用了trim()。使用Json内置的ConvertFrom是一种更好的方法。它提高了可读性并减少了容易出错的子字符串操作。当Json模式发生变化时,使用ConvertFrom Json是一个更具可读性的解决方案,供工程师再次使用。
$jsonObj = $jsonString | ConvertFrom-Json
$jsonObj.objects.sbbuildid
$sb_build_id = $build_info.Substring($build_info.IndexOf("sbbuildid") + 11, 8).trim()