调用WebRequest嵌套的响应格式



第一次发帖,如果我错过了这里的一些礼仪,我很抱歉。我已经开始在Powershell中使用WebRequest来整理下面JSON(见下文(的一些有用部分,最终目的是将其链接到GUI。

我正在运行以下

$response = Invoke-WebRequest 'https://directory.spineservices.nhs.uk/ORD/2-0-0/organisations/P81085'|convertFrom-Json |select -expand Organisation

完整响应以JSON形式返回,如下所示:

{
"Organisation": {
"Name": "YORK BRIDGE SURGERY",
"Date": [
{
"Type": "Operational",
"Start": "1974-04-01",
"End": "2018-05-02"
},
{
"Type": "Legal",
"Start": "1974-04-01",
"End": "2018-04-30"
}
],
"OrgId": {
"root": "2.16.840.1.113883.2.1.3.2.4.18.48",
"assigningAuthorityName": "HSCIC",
"extension": "P81085"
},
"Status": "Inactive",
"LastChangeDate": "2018-05-04",
"orgRecordClass": "RC1",
"GeoLoc": {
"Location": {
"AddrLn1": "5 JAMES STREET",
"Town": "MORECAMBE",
"County": "LANCASHIRE",
"PostCode": "LA4 5TE",
"Country": "ENGLAND"
}

在PS中显示为:

Date           : {@{Type=Operational; Start=1974-04-01; End=2018-05-02}, @{Type=Legal; Start=1974-04-01; End=2018-04-30}}
OrgId          : @{root=2.16.840.1.113883.2.1.3.2.4.18.48; assigningAuthorityName=HSCIC; extension=P81085}
Status         : Inactive
LastChangeDate : 2018-05-04
orgRecordClass : RC1
GeoLoc         : @{Location=}
Contacts       : @{Contact=System.Object[]}
Roles          : @{Role=System.Object[]}
Rels           : @{Rel=System.Object[]}

我可以通过设置以下内容来分配变量并调用各个部分:

$name = $response.Name
$date = $response.date

哪个返回:

YORK BRIDGE SURGERY
Type        Start      End       
----        -----      ---       
Operational 1974-04-01 2018-05-02
Legal       1974-04-01 2018-04-30

但是,如果我设置:

$address = $response.geoloc

我得到以下

Location-------- @{AddrLn1=5 JAMES STREET; Town=MORECAMBE; County=LANCASHIRE; PostCode=LA4 5TE; Country=ENGLAND}

有人能建议我如何只返回位置数据的各个部分吗?理想情况下,我希望能够设置$town=$response.town,并让它只返回";莫尔坎贝";然而,在这个例子中,我显然遗漏了一些明显的

$response.Organisation.geoloc.Location
$response.Organisation.geoloc.Location.County

等等

已解决

对于将来可能有同样问题的人:

$addrLn1 = $response.geoloc.location.addrLn1
$addrLn2 = $response.geoloc.location.addrLn2
$town = $response.geoloc.location.town

允许我设置我需要的变量,这会返回我需要的单个数据。

回想起来,很明显,但我仍在起步,希望这有一天能帮助其他人

最新更新