json - Invoke-WebRequest 嵌套响应格式
问题描述
如果我在这里错过了一些礼仪,第一次如此道歉。我已经开始在 Powershell 中使用 WebRequests 来整理下面的 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 并让它在这个例子中只返回“Morecambe”但是我显然错过了一些明显的东西
解决方案
$response.Organisation.geoloc.Location
$response.Organisation.geoloc.Location.County
等等
推荐阅读
- apache-nifi - How to get NiFi processor error into Flowfile attribute?
- shell - 在 shell 脚本中运行 pip 和 conda:未找到
- postgresql - SELECT 中包含多个表的不明确列引用“ctid”
- amazon-dynamodb - 尝试从无服务器堆栈中导出 dynamodb 表变量失败,内部函数!Ref
- javascript - Vue - 在表单上填写了多少字段的百分比
- reactjs - 从按钮调用时反应渲染没有改变
- r - 因子错误(数据集 $ 名称,级别 = c(“扎克威尔逊:杰伦吉普森”),:无效的“标签”;长度 269 应为 1 或 1
- r - 识别空间点是否在空间多边形中
- php - 将子元素值作为 json 从数据库中的 xml 插入
- sql - 如何删除重复的日期时间并将一列值转换为新的列名?