批处理文件,将json文件读入字符串

时间:2017-09-16 13:08:57

标签: batch-file

我需要读一个json文件:

{
   "key": {
       "subkey": "value"
   }
}

所以我可以将它作为参数传递给命令:

program -e SETTINGS=<JSON FILE AS STRING>

1 个答案:

答案 0 :(得分:3)

结合这些问题的答案......

...我们可以编写以下批处理文件:

:: Make it possible to read immediate value of variable using !variable! syntax.
setlocal enabledelayedexpansion

:: Read file "test.json" into variable data, removing line breaks.
set data=
for /f "delims=" %%x in (test.json) do set "data=!data!%%x"

:: Escape double quotes in data
set data=%data:"=\"%

:: Finally call program with the entire content of the JSON file as parameter
program -e "SETTINGS=%data%"

请注意,使用cmd.exe命令处理器maximum length for a commandline and also for environment variables is 8191 characters时,这显然会限制您可以传递的JSON文件的最大大小。