简体   繁体   English

将 json 文件字段的内容传递给 powershell 中的变量

[英]pass contents of a json file field to a variable in powershell

I have a json file and I want to pass the value of a field to a variable the json file has the following structure我有一个 json 文件,我想将字段的值传递给变量 json 文件具有以下结构

{
"actions": {
   "send_dian": false,
   "send_email": false
 },
  "invoice": {
    "number": "990001344",
    "invoice_type_code": "FACTURA_VENTA",
    "numbering": {
       "resolution_number": "18760000001",
       "prefix": "MSTL",
       "flexible": true
       },
    "customer": {
       "email": "TOROFABIAN@GMAIL.COM",
       "phone": "3136490333",
       "company_name": "LUIS FABIAN TORO GUTIERREZ"
       }
   }
  }

and I want to pass the value of the "number" field to a variable like this:我想将“数字”字段的值传递给这样的变量:

$archivo = "archivo1.json"
$body = Get-Content $archivo 
$numerofolio1 = $body.psobject.properties["number"].value
Write-Host "numero de folio:" $numerofolio1
$numerofolio1

$numerofolio2 = $body.psobject.properties["invoice.number"].value
Write-Host "numero de folio:" $numerofolio2
$numerofolio2

but neither of the two ways sends me the value of "number".但是这两种方式都没有向我发送“数字”的值。

how should I do it?我该怎么做? Thanks谢谢

You need to convert the json to objects with ConvertFrom-Json :您需要使用ConvertFrom-Json将 json 转换为对象:

$body = Get-Content $archivo |ConvertFrom-Json

$body.invoice.number # this will now give you the string "990001344"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM