将JSON转换为Powershell对象会输出奇怪的输出

时间:2018-10-11 13:58:52

标签: json api powershell

此刻我正在努力使用api。 我尝试从api读取信息,并在powershell中使用这些信息。该API是JSON。 问题在于,它始终具有此“ @ {label = id; value =”双重信息,此后某些值为NULL,但其中应该包含信息。 预先谢谢你。

希望是可以理解的:)

## Parameter ##
$clientID = "ABCD"
$clientSecret = "EVGH"
$urlAPI = "https://api.url.de/..."

try {
    $result = Invoke-RestMethod -Uri ("https://api.url.de/v1/auth?client_id=" + $clientID + "&client_secret=" + $clientSecret) -Method POST  -ErrorAction stop
}
catch {Write-Host "Webrequest broke"}

$token = $result.data
$token = $token.token

$header = @{Authorization = 'Bearer ' + $token}
try {
    $result = Invoke-RestMethod -Method 'Get' -URI $urlAPI  -Headers $header -ErrorAction stop 
}
catch {Write-Host "Webrequest 2 broke"}

$result.data.attributes

获取的信息应如下所示:

{"success":true,"data":[{
    {"type":"Employee","attributes":
        {"id":
            {"label":"id","value":999999},
        "first_name":
            {"label":"First name","value":"John"},
        "last_name":
            {"label":"Last name","value":"Doe"},
        "position":
            {"label":"Position","value":"Employee"},
        "hire_date":
            {"label":"Hire date","value":"2001-01-01T00:00:00+02:00"},
        "subcompany":
            {"label":"Subcompany","value":
                {"type":"Subcompany","attributes":
                    {"id":1234,"name":"Company"}}},
        "department":
            {"label":"Department","value":
                {"type":"Department","attributes":
                    {"id":23456,"name":"Department-Superfun"}}}
}}}]}

$ result.data.attributes的返回看起来像这样:

id               : @{label=id; value=999999}
first_name       : @{label=First name; value=John}
last_name        : @{label=Last name; value=Doe}
position         : @{label=Position; value=Employee}
hire_date        : @{label=Hire date; value=2001-01-01T00:00:00+01:00}
subcompany       : @{label=Subcompany; value=}
department       : @{label=Department; value=}

1 个答案:

答案 0 :(得分:0)

您看到的值实际上是正确的。如果查看数据的结构,则属性是具有属性id,first_name,last_name等的对象。这些属性的值本身就是对象,具有自己的属性。例如,attributes.id的值是具有属性标签和值的对象。

对于子公司,它具有一个名为value的属性,其值是一个对象。 PowerShell默认情况下不显示嵌套对象的内容,但是如果您尝试使用PowerShell访问属性,则它们将存在。部门也是如此。