2017-05-30 77 views
2

我需要从Powershell中的json读取一些信息。我遇到的问题是,在“路径”部分之后,我无法指定下一个名称,例如调用路径是因为它发生了变化。之后,我需要获取“获取”部分,然后获取“参数”部分,最后获取名称和必填字段。如何在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

我用这个得到它:

for($i=0;$i-lt$pName.Count;$i++) 
      { 
       $test | Select -ExpandProperty paths | select -ExpandProperty $pName[$i] | select -ExpandProperty get 

      } 
0

您的JSON文件没有明确的格式化和思念某右括号。 然而,关闭JSON文件,并读入的PowerShell(ConvertFrom-Json)后,您PowerShell的物体看起来像这样(使用PSONWrite-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