【问题标题】:Altering values in .json files更改 .json 文件中的值
【发布时间】:2015-07-27 23:41:48
【问题描述】:

我有一个简单的问题。 我有一个包含以下内容的 config.json 文件

"DefaultAdminUsername": "Administrator@test.com",
"DefaultAdminPassword": "Admin!",
"DefaultNoOfUsers": "100",

我想从批处理文件中修改“DefaultNoOfUsers”的值 例如:从 100 变为 1000

伪代码可以是

for /f %%A in (config.json) do (

    SET key = value
)

echo %VERSION%
echo %TEST%

【问题讨论】:

  • 这和bash有什么关系?
  • 好的,我需要一种方法来配置我的 .net 应用程序要创建的用户数。例如,当我的应用程序启动时,它将在数据库中创建 100 个用户条目.....这就像为我的应用程序创建工作负载一样。所以为此我想在 Config.json 中添加“DefaultNoOfUser”。并且可以修改此值以创建工作负载。示例 10 个用户。 100个用户甚至1000个用户
  • 不清楚你在问什么。如果有帮助,您可以使用 JScript 方法读取和操作 JSON 字符串,然后将 obj.toString() 写入新的(或覆盖现有的)JSON 文件。 More info.

标签: batch-file


【解决方案1】:

这是一个带注释的批处理文件,用于在文件config.json 中进行此替换而不检查输入错误。

@echo off
rem Exit this batch file if file config.json does not exist in current directory.
if not exist config.json goto :EOF

rem Make sure that the temporary file used does not exist already.
del "%TEMP%\config.json" 2>nul

rem Define a default value for number of users.
set "DefaultNoOfUsers=100"

rem Ask user of batch file for the number of users.
set /P "DefaultNoOfUsers=Number of users (%DefaultNoOfUsers%): "

rem Copy each line from file config.json into temporary file
rem with replacing on line with "DefaultNoOfUsers" the number.
for /F "tokens=1* delims=:" %%A in (config.json) do (
    if /I %%A == "DefaultNoOfUsers" (
        echo %%A: "%DefaultNoOfUsers%",>>"%TEMP%\config.json"
    ) else (
        echo %%A:%%B>>"%TEMP%\config.json"
    )
)

rem Move the temporary file over file config.json in current directory.
move /Y "%TEMP%\config.json" config.json >nul

rem Delete the used environment variable.
set "DefaultNoOfUsers="

在命令提示符窗口for /? 中运行并阅读窗口中的整个帮助输出,以了解 for 循环的工作原理。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-08
    • 1970-01-01
    • 2021-12-17
    • 2016-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多