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

如何通过Windows Shell批处理文件修改JSON指定字段值为0

用Windows批处理(.bat)修改JSON中指定字段值为0

以下是一个可批量将JSON文件中"NativeOffsetX"和"NativeOffsetY"字段对应值统一改为"0"的批处理脚本:

@echo off
setlocal enabledelayedexpansion

:: 设置要处理的JSON文件路径,可替换为目标文件,支持通配符如*.json
set "json_file=target.json"

:: 调用PowerShell执行正则替换,原生批处理对复杂文本替换支持有限
powershell -Command "(Get-Content '%json_file%') -replace '\"NativeOffsetX\":\s*\"[0-9]+\.[0-9]+\"', '\"NativeOffsetX\": \"0\"' -replace '\"NativeOffsetY\":\s*\"[0-9]+\.[0-9]+\"', '\"NativeOffsetY\": \"0\"' | Set-Content '%json_file%'"

echo 处理完成!
endlocal

脚本说明:

  • @echo off:关闭命令回显,让输出更简洁
  • setlocal enabledelayedexpansion:启用延迟环境变量扩展,确保变量处理正常
  • set "json_file=target.json":指定目标JSON文件,可修改为具体路径(如C:\config\settings.json)或用*.json处理当前目录下所有JSON文件
  • 核心PowerShell命令逻辑:
    • Get-Content '%json_file%':读取文件内容
    • -replace:通过正则匹配"NativeOffsetX": "0.xxxx"格式的内容,替换为"NativeOffsetX": "0",同理处理NativeOffsetY字段
    • Set-Content '%json_file%':将修改后的内容写回原文件

使用注意:

  • 系统需自带PowerShell(Windows 7及以上默认预装)
  • 处理前建议备份JSON文件,避免数据意外丢失
  • 若字段值格式有变化(如无引号的整数),可调整正则表达式,比如把\"[0-9]+\.[0-9]+\"改为[0-9]+(\.[0-9]+)?

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 21:25:24