使用批处理脚本从json文件中读取URL

时间:2021-06-02 23:31:15

I am reading a config json file through batchscript and creating variables dynamically.

我正在通过batchscript读取配置json文件并动态创建变量。

Content of json file is given below

json文件的内容如下

{ 
"ApplicationId":"c2b925c2-e5c9-4534-b855-43534",
"ApplicationProcess":"453453-ca1c-4735-806a-45345",
"VersionComponentId":"4533-35d2-4f0c-bb7a-rtert",
"uDClientFolder":"/udclient/",
"FID":"myId",
"FAuthToken":"mypassword",
"uDeployUrl":"https://myurl:8445",
"outPutDir":"..\Binaries\_PublishedWebsites\OutPut",
}

Batch script to read variables is given below

下面给出了读取变量的批处理脚本

for /f "tokens=1,2 delims=:, " %%a in (' find ":" ^< ".\%jsonConfigFile%" ') do (


   set a=%%~a: =%
   set b=%%~b: =%
   set "%%~a=%%~b"
)

Here i am facing two problems. 1. Unable to read uDeployUrl because it contains ://. I am getting only https part of the url. 2. If my json contains space before the keyname like "Application":"value" Then variable name will also contain space in it's name. So how can i remove starting space from variable name

在这里,我面临两个问题。 1.无法读取uDeployUrl,因为它包含://。我只获得了网址的https部分。 2.如果我的json在keyname之前包含空格,比如“Application”:“value”那么变量名也将在其名称中包含空格。那么如何从变量名中删除起始空间

Thanks in advance.

提前致谢。

1 个解决方案

#1


-1  

for /f "tokens=1* delims=:" %%a in ('find ":" "%jsonConfigFile%"') do set "%%~a=%%~b
  • Use tokens=1* to get everything after : into %%b
  • 使用tokens = 1 *获取之后的所有内容:into %% b

  • Use ~ prefix to strip the surrounding quotes from %%a and the opening quote from %%b
  • 使用~prefix去除%% a中的周围引号和%% b中的开头引号

  • Since the last quote in %%b is followed by , it won't be removed but we can simply enclose the entire set assignment into doublequotes by prefixing it with one doublequote so that the final assignment will be for example set "uDeployUrl=https://myurl:8445", - the last comma will be ignored.
  • 由于后面跟着%% b中的最后一个引号,它不会被删除,但是我们可以简单地将整个集合赋值括在双引号中,前缀为一个双引号,以便最后的赋值将设置为“uDeployUrl = https: // myurl:8445“, - 最后一个逗号将被忽略。

  • find's second parameter is a file name so no need to use input redirection via <
  • find的第二个参数是文件名,因此不需要通过 <使用输入重定向< p>

#1


-1  

for /f "tokens=1* delims=:" %%a in ('find ":" "%jsonConfigFile%"') do set "%%~a=%%~b
  • Use tokens=1* to get everything after : into %%b
  • 使用tokens = 1 *获取之后的所有内容:into %% b

  • Use ~ prefix to strip the surrounding quotes from %%a and the opening quote from %%b
  • 使用~prefix去除%% a中的周围引号和%% b中的开头引号

  • Since the last quote in %%b is followed by , it won't be removed but we can simply enclose the entire set assignment into doublequotes by prefixing it with one doublequote so that the final assignment will be for example set "uDeployUrl=https://myurl:8445", - the last comma will be ignored.
  • 由于后面跟着%% b中的最后一个引号,它不会被删除,但是我们可以简单地将整个集合赋值括在双引号中,前缀为一个双引号,以便最后的赋值将设置为“uDeployUrl = https: // myurl:8445“, - 最后一个逗号将被忽略。

  • find's second parameter is a file name so no need to use input redirection via <
  • find的第二个参数是文件名,因此不需要通过 <使用输入重定向< p>