我需要从Powershell的json中读取一些信息。我遇到的问题是"路径"第一节我不能指定下一个名字,例如调用路径是因为它发生了变化。然后我需要得到"得到"部分,然后是"参数"部分,最后得到名称和必填字段。
我可以使用$info.host
和$info.paths
之类的路径获取主机名。然后我通过遍历路径得到各个路径名称,以获得这样的每个名称。
foreach($item in $pName)
{
$item
}
但那就是我可以得到我尝试使用$ item.childItem,但它不起作用。无论如何使用$ item.paths.childItems [$ 1] .get.parameters之类的东西,然后获取名称和必填字段值?
json看起来像这样:
{
"swagger": "2.0",
"info": {
"version": "v1",
"title": "Requirements.API"
},
"host": "replica.net",
"schemes": [
"http"
],
"paths": {
"/": {
"get": {
"tags": [
"Alive"
],
"operationId": "Alive_Get",
"consumes": [],
"produces": [
"application/json",
"text/json",
"application/xml",
"text/xml"
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/Object"
}
}
},
"deprecated": false
}
},
"/requirements/email-docs": {
"get": {
"tags": [
"CustomerRequirement"
],
"operationId": "",
"consumes": [],
"produces": [
"application/json",
"text/json",
"application/xml",
"text/xml"
],
"parameters": [
{
"name": "name",
"in": "query",
"required": true,
"type": "integer",
"format": "int32"
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/UploadResponse"
}
}
}
},
"deprecated": false
}
},
答案 0 :(得分:0)
我用它来得到它:
for($i=0;$i-lt$pName.Count;$i++)
{
$test | Select -ExpandProperty paths | select -ExpandProperty $pName[$i] | select -ExpandProperty get
}
答案 1 :(得分:0)
您的JSON文件格式不清晰,并且遗漏了一些右括号。
然而,在关闭JSON文件并将其读入PowerShell(ConvertFrom-Json
)后,您的PowerShell对象如下所示(使用PSON或Write-Log):
{
swagger: "2.0",
info: {
version: "v1",
title: "Requirements.API"
},
host: "replica.net",
schemes: @(
"http"
),
paths: {
/: {
get: {
tags: @(
"Alive"
),
operationId: "Alive_Get",
consumes: @(),
produces: @(
"application/json",
"text/json",
"application/xml",
"text/xml"
),
responses: {
200: {
description: "OK",
schema: {
$ref: "#/definitions/Object"
}
}
},
deprecated: $False
}
},
/requirements/email-docs: {
get: {
tags: @(
"CustomerRequirement"
),
operationId: "",
consumes: @(),
produces: @(
"application/json",
"text/json",
"application/xml",
"text/xml"
),
parameters: @(
{
name: "name",
in: "query",
required: $True,
type: "integer",
format: "int32"
}
),
responses: {
200: {
description: "OK",
schema: {
type: "array",
items: {
$ref: "#/definitions/UploadResponse"
}
}
}
},
deprecated: $False
}
}
}
}
所以我猜你要找的属性/值是:
$Item.paths.PSObject.Properties | Select -ExpandProperty Name