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

G33kDude cJson加载JSON至AHK后,如何访问带点键并修改保存?

用AHK修改VS Code工作区配置的终端字体大小

原始JSON配置文件内容

{    
    "folders": [        
        {            
            "path": "."        
        }    
    ],    
    "settings": {            
        "files.exclude": {            
            "**/.git": true,            
            "**/.svn": true,            
            "**/.hg": true,            
            "**/CVS": true,            
            "**/.DS_Store": true,            
            "**/Thumbs.db": true,            
            "**.lnk":true,            
            "*.code-workspace" :true,            
            "**.vscode/":true,            
            "**.url":true        
        },        
        "terminal.integrated.fontSize": 12    
    }
}

需求

仅针对terminal.integrated.fontSize这个键,对其值(当前为12)进行增减操作后,覆盖保存回原文件。

遇到的问题

尝试了以下AHK代码,但无法获取该键的值:调试器显示对象包含该键,但OutputDebug无任何输出。

path    := "C:\temp\Rest APIs.code-workspace"
Obj     := Json.Load(FileOpen(Path,"r").Read())
OutputDebug, % Obj.settings
OutputDebug, % Obj.settings[1]
OutputDebug, % Obj.settings[2]

解决方案

问题出在键名包含.,AHK中直接用.访问会被解析为对象层级,必须用引号包裹键名来访问:

  1. 获取值的正确方式:
fontSize := Obj.settings["terminal.integrated.fontSize"]
OutputDebug, % fontSize  ; 此时会输出12
  1. 修改值并保存回文件的完整代码示例:
path := "C:\temp\Rest APIs.code-workspace"
; 读取并解析JSON文件
file := FileOpen(path, "r")
jsonStr := file.Read()
file.Close()
Obj := Json.Load(jsonStr)

; 修改字体大小(示例为增加2,可自行调整增减逻辑)
Obj.settings["terminal.integrated.fontSize"] += 2

; 将修改后的对象转回格式化JSON并保存
newJsonStr := Json.Dump(Obj, {indent: "    "})  ; 保留4空格缩进,和原文件风格一致
file := FileOpen(path, "w", "UTF-8")
file.Write(newJsonStr)
file.Close()

关键说明

  • 当键名包含.这类特殊字符时,AHK对象不能用Obj.key.subkey的层级访问方式,必须用Obj["key.subkey"]的形式直接指定完整键名。
  • Json.Dump的indent参数可以让输出的JSON保持格式化,避免变成一行压缩文本,方便后续查看编辑。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 07:32:42