You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在PowerShell中将JSON中的字符串数组转为字符串数组?代码故障排查

Convert JSON String Array to PowerShell String Array

Let's fix your code and get that JSON array into a proper PowerShell string array. First, let's break down what's going wrong with your current approach, then walk through the correct solution.

Issues in Your Current Code

  • Incorrect xyz property access: Your $json.abc is an array of objects (each with def, ghi, or xyz respectively). You can't directly use $json.abc.xyz because that tries to fetch the xyz property from every element in the abc array—this property only exists on the third element.
  • Unnecessary global variable: Using $global:array is poor practice here. Functions should return values instead of modifying global state.
  • Redundant Out-String: Get-Content with the -Raw parameter reads the entire file as a single string, which works directly with ConvertFrom-Json without needing extra string conversion.

Correct Solution

Let's walk through a clean, robust way to extract the string array:

Step 1: Parse the JSON File

Simplify the JSON reading process:

# Read and parse the JSON in one concise step
$json = Get-Content "path to json file" -Raw | ConvertFrom-Json

Step 2: Extract the xyz String Array

Since $json.abc is an array, we need to target the specific object that holds the xyz property. Here are two reliable methods:

Option 1: Target by Index (for fixed positions)

If you know the xyz object is always the third element (PowerShell uses zero-based indexing, so index 2):

$stringArray = $json.abc[2].xyz

Option 2: Dynamic Lookup (for variable positions)

If the position of the xyz object might change, use Where-Object to find it dynamically:

$xyzObject = $json.abc | Where-Object { $_.PSObject.Properties.Name -contains 'xyz' }
$stringArray = $xyzObject.xyz

Step 3: Rewrite Your Function (Best Practice)

If you want to wrap this logic in a function, make it return the array instead of relying on global variables:

function Get-XyzStringArray {
    param(
        [Parameter(Mandatory=$true)]
        $JsonInput
    )
    # Locate the object with the xyz property
    $xyzObject = $JsonInput.abc | Where-Object { $_.PSObject.Properties.Name -contains 'xyz' }
    # Return the string array
    return $xyzObject.xyz
}

# Usage example:
$json = Get-Content "path to json file" -Raw | ConvertFrom-Json
$stringArray = Get-XyzStringArray -JsonInput $json

# Verify the result
$stringArray
# Check the type to confirm it's a string array
$stringArray.GetType()

Test the Result

When you run this code, $stringArray will be a proper PowerShell string array containing:

[dbo].[abc1]
[dbo].[def1]

You can confirm it's an array by checking $stringArray.Count (it will return 2) or looping through it with a foreach statement.


内容的提问来源于stack exchange,提问作者Ronnie Kapoor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:07:34