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

如何将指定格式TXT解析为JSON?PowerShell技术咨询

问题:将特定格式TXT解析为JSON及PowerShell命令相关疑问

需求说明

我花了一整天尝试解决这个问题,恳请帮助!需要将以下格式的TXT文件解析为指定结构的JSON。

输入TXT格式

============ Get Mouse Position  ===============
    Get Mouse Position
    GetMousePos
    Get the mouse position first
        Command one
        Command two
        Command three
============ Set Mouse Position  ===============
    Set Mouse Position
    SetMousePos
    Set the mouse position after
        Command one
        Command two
        Command three

目标JSON输出

{
  "Get Mouse Position": {
    "Trigger": "GetMousePos",
    "Description": "Get the mouse position first",
    "body": [
      "Command one",
      "Command two",
      "Command three"
    ]
  },
  "Set Mouse Position": {
    "Trigger": "SetMousePos",
    "Description": "Set the mouse position after",
    "body": [
      "Command one",
      "Command two",
      "Command three"
    ]
  }
}

遇到的问题

  • 使用ConvertFrom-StringData命令尝试过,结果接近需求,但该命令似乎不支持单键对应数组(即Command one这类列表内容)。
  • 使用PSCustomObject结合ConvertTo-Json可以生成完全符合要求的JSON,但无法编写批量处理所有===== [Object] =====这类区块的代码。

疑问

ConvertFrom-StringData命令是否不支持单键对应多值/数组?

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 11:18:20