栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何使用Powershell遍历JSON属性

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何使用Powershell遍历JSON属性

以下代码定义并使用 function

Get-FirstPropertyValue
,该 函数 对具有给定名称的对象图内的第一个属性
执行 深度优先递归搜索 ,并假设其值为非null:

# Function that returns the value of the first property with the given # name found during recursive depth-first traversal of the given object.# Note that null-valued properties are ignored.function Get-FirstPropertyValue($obj, $propName) {  $propNames = $obj.psobject.properties.Name  if ($propName -in $propNames) {    $obj.$propName  } else {    foreach ($iterPropName in $propNames) {        if ($null -ne ($val = Get-FirstPropertyValue $obj.$iterPropName $propName)) {          return $val        }      }  }}# Input JSON$json = @'{    "nodes": {        "oTUltX4IQMOUUVeiohTt8A": { "name": "H5dfFeA", "transport_address": "127.0.0.1:9300", "host": "127.0.0.1", "ip": "127.0.0.1:9300", "tasks": {     "oTUltX4IQMOUUVeiohTt8A:124": {         "node": "oTUltX4IQMOUUVeiohTt8A",         "id": 124,         "type": "direct",         "action": "cluster:monitor/tasks/lists[n]",         "start_time_in_millis": 1458585884904,         "running_time_in_nanos": 47402,         "cancellable": false,         "parent_task_id": "oTUltX4IQMOUUVeiohTt8A:123"     } }        }    }}'@# Convert the JSON to a [pscustomobject] graph with ConvertFrom-Json.$objFromJson = $json | ConvertFrom-Json# Using the function defined above, get the first 'tasks' object found# during recursive depth-first traversal.$tasks = Get-FirstPropertyValue $objFromJson 'tasks'# Get the name of the resulting object's first property.$propName = @($tasks.psobject.properties.Name)[0]# Extract the .id property from the object stored in the first property.$tasks.$propName.id

以上收益:

124

一个 更简洁但更晦涩且可能更慢的替代方法将JSON输入转换为XML,然后使用 XPath对其 进行查询:

# Input JSON$json = @'{    "nodes": {        "oTUltX4IQMOUUVeiohTt8A": { "name": "H5dfFeA", "transport_address": "127.0.0.1:9300", "host": "127.0.0.1", "ip": "127.0.0.1:9300", "tasks": {     "oTUltX4IQMOUUVeiohTt8A:124": {         "node": "oTUltX4IQMOUUVeiohTt8A",         "id": 124,         "type": "direct",         "action": "cluster:monitor/tasks/lists[n]",         "start_time_in_millis": 1458585884904,         "running_time_in_nanos": 47402,         "cancellable": false,         "parent_task_id": "oTUltX4IQMOUUVeiohTt8A:123"     } }        }    }}'@$parent = 'tasks'$prop = 'id'$propType = 'int'$json |   ConvertFrom-Json |    ConvertTo-Xml -Depth ([int]::MaxValue) |       Select-Xml "//Property[@Name='$parent']/*/*[@Name='$prop']/text()" |        ForEach-Object { $_.Node.InnerText -as $propType }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/393246.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号