如何在JSON文件上的PowerShell中输入新闻方括号 [英] How to enter press square brackets in PowerShell on JSON File

查看:66
本文介绍了如何在JSON文件上的PowerShell中输入新闻方括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用PowerShell查询JSON文件中类型"字符串的更改值?我找不到类型"字符串.

How can I query change value of the "type" string in my JSON file with PowerShell? I can't get to the "type" string.

{
"name":  "b",
"compatibilityLevel":  1400,
"model":  {
              "culture":  "c",
              "dataSources":[
                                  {
                                      "type":  "structured"
                                  }
                            ]
         }}

PowerShell

$pathToJson =  "C:\Model.bim"

$a = Get-Content $pathToJson | ConvertFrom-Json

$a.'model.dataSources.type' = "c"

$a | ConvertTo-Json -Depth 10  | Set-Content $pathToJson

推荐答案

tl; dr

$a.model.dataSources[0].type = 'c'

请注意需要指定索引 [0] ,因为 $ a.model.dataSources 是一个 array .

Note the need to specify index [0], because $a.model.dataSources is an array.

针对您尝试过的操作的AS:

$ a.'model.dataSources.type'="c"

  • 您不能使用存储在 string ('...')中的属性 path 直接访问嵌套属性,因为PowerShell将'model.dataSources.type'解释为单个属性的名称.

    • You cannot use a property path stored in a string ('...') to directly access a nested property, because PowerShell interprets 'model.dataSources.type' as the name of a single property.

      即使已解决该问题, $ a.model.dataSources.type ="c" 也不起作用,因为 $ a.model.dataSources 返回值的数组,并且您不能直接在该数组的 elements 设置一个属性;取而代之的是,您必须明确地将目标数组元素作为目标,如上所示( [0] ).

      Even with that problem corrected, $a.model.dataSources.type = "c" does not work, because $a.model.dataSources returns an array of values, and you cannot directly set a property on that array's elements; instead you must explicitly target the array element of interest, as shown above ([0]).

      • 请注意,您可以通过称为$ a.model.dataSources.type 的PSv +功能,获取数组元素的 .type 值.成员枚举,但这不适用于设置-通过设计,以防止可能的无意具有相同值的所有数组元素的更新;参见此答案.
      • Note you can get the array elements' .type values with $a.model.dataSources.type, via a PSv+ feature called member enumeration, but that doesn't work on setting - by design, to prevent possibly inadvertent updating of all array elements with the same value; see this answer.

      这篇关于如何在JSON文件上的PowerShell中输入新闻方括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆