简体   繁体   中英

How to read the values in json if there is a dash(-) in keys using powershell

Given this Json:

{
 “StudentInfo":{
   “first-name": “xyz",
   “Student_id": "123-xyz"
 }
}

I could read the value for student_id using the code below.

$config = Get-Content -Raw $Config
$configObject = ConvertFrom-Json –InputObject $config
$StudentId = $configObject.StudentInfo.Student_id

However, I am unable to read the value for "first-name" as powershell is treating it to be a command instead of variable.Changing key name is not an option for me.

How can we retrieve the value for "first-name" using powershell 5.1?

使用引号:

$configObject.StudentInfo.'first-name'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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