PowerShell-访问JObject内的JArray [英] PowerShell -- Accessing a JArray inside a JObject
本文介绍了PowerShell-访问JObject内的JArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个Json对象
{
"ProjectDirectory": "C:\\Main",
"SiteName": "RemoteOrder",
"ParentPath": "/Areas//Views",
"VirtualDirectories": [
{
"Name": "Alerts",
"Path": "\\Areas\\RemoteOrder\\Views\\Alerts"
},
{
"Name": "Analytics",
"Path": "\\Areas\\RemoteOrder\\Views\\Analytics"
},
{
"Name": "Auth",
"Path": "\\Areas\\RemoteOrder\\Views\\Auth"
}
]
}
我创建的
$config = [Newtonsoft.Json.Linq.JObject]::Parse($file)
我可以访问
$config["ProjectDirectory"]
$config["VirtualDirectories"]
但是我无法到达VirtualDirectories JArray内部的元素
But I can not get to the element inside the VirtualDirectories JArray
我确认
$config["VirtualDirectories"][0].GetType() // JObject
$config["VirtualDirectories"].GetType() // JArray
$config // JObject
我尝试过
$config["VirtualDirectories"][0]["Name"]
$config["VirtualDirectories"][0]["Path"]
$config["VirtualDirectories"][0][0]
$config["VirtualDirectories"][0].GetValue("Name")
当我尝试
$config["VirtualDirectories"][0].ToString()
我知道
{
"Name": "Alerts",
"Path": "\\Areas\\RemoteOrder\\Views\\Alerts"
}
我真正想做的是访问它的一个循环,但是我似乎再也无法访问JObject Elements
What I am really trying to do is access it a loop but again I can not seem to access the JObject Elements
推荐答案
您很亲密. $config["VirtualDirectories"][0]["Name"]
将为您提供一个包含文本的JValue
.您只需要从此处使用Value
属性即可获取实际的字符串.这是在ForEach
循环中执行的操作:
You are close. $config["VirtualDirectories"][0]["Name"]
will give you a JValue
containing the text. You just need to use the Value
property from there to get the actual string. Here is how you would do it in a ForEach
loop:
$config = [Newtonsoft.Json.Linq.JObject]::Parse($file)
ForEach ($dir in $config["VirtualDirectories"])
{
$name = $dir["Name"].Value
$path = $dir["Path"].Value
...
}
这篇关于PowerShell-访问JObject内的JArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文