英文:
I need to parse particular json file but stucked
问题
,,,"halloween,pumpkin,nature,animal"
setlocal enabledelayedexpansion
set keywords=""
rem Loop through each line of the keywords.txt file
for /f "skip=1 tokens=2 delims={" %%b in (keywords.txt) do (
rem Extract the keyword from the current line
set keyword=%%b
set keyword=!keyword:"keyword":!
set keyword=!keyword:",score":!
set keyword=!keyword:,!
rem Add the keyword to the keywords variable
set keywords=!keywords!,!keyword!
)
But it doesn't help anything.
英文:
There are json file with variable quantity of elements describing the keywords. The following is the typical example:
{"keywords":[{"keyword":"halloween","score":0.9621220167107003},
{"keyword":"pumpkin","score":0.9527655356551675},
{"keyword":"nature","score":0.8530320061364176},
{"keyword":"animal","score":0.7936456829239327}],"status":"ok"}
The script should parce this json and I need to get a line with keywords formatted as following:
,,,"halloween,pumpkin,nature,animal"
As I already stated, the number of entries may be different. From 10 to 100, for example.
Honestly, I am stuck with that task. Would anyone help me please?
setlocal enabledelayedexpansion
set keywords=""
rem Loop through each line of the keywords.txt file
for /f "skip=1 tokens=2 delims={" %%b in (keywords.txt) do (
rem Extract the keyword from the current line
set keyword=%%b
set keyword=!keyword:"keyword":"!
set keyword=!keyword:",score":!
set keyword=!keyword:,!
rem Add the keyword to the keywords variable
set keywords=!keywords!,!keyword!
)
But it doesn't help anything
答案1
得分: 1
你已经包含了标签[tag:parsing],但由于Batch不支持JSON,我希望你意识到,如果不使用脆弱的方法,无法使用它来解析JSON。
我建议您使用专门的JSON工具,比如[tag:xidel]:
xidel -s "keywords.txt" -e "$json//keyword" --output-separator=,
xidel -s "keywords.txt" -e "join($json//keyword,',')"
如果您真的想要周围的双引号,以下任何一个都可以:
xidel -s "keywords.txt" -e "concat('\"',join($json//keyword,','),'\"')"
xidel -s "keywords.txt" -e "'\"'||join($json//keyword,',')||'\"'"
xidel -s "keywords.txt" -e "x'\"{join($json//keyword,',')}\"'"
这是第三方网站API的答案(everypixel.com)
如果您是通过GET请求或POST请求(使用curl
?)获得的JSON,那么xidel
也可以帮助您,无需将其保存到临时文件。
英文:
You've included the tag [tag:parsing], but as Batch doesn't support JSON at all, I hope you realize that you can't use it to parse JSON without resorting to fragile hacks.
I'd recommend you use a dedicated JSON tool instead, like [tag:xidel]:
xidel -s "keywords.txt" -e "$json//keyword" --output-separator=,
xidel -s "keywords.txt" -e "join($json//keyword,',')"
And if you really want the surrounding double-quotes, either of these will do:
xidel -s "keywords.txt" -e "concat('\"',join($json//keyword,','),'\"')"
xidel -s "keywords.txt" -e "'"'||join($json//keyword,',')||'"'"
xidel -s "keywords.txt" -e "x'\"{join($json//keyword,',')}\"'"
> it is answer of third party site's API (everypixel.com)
If it's simply through a GET- or POST-request (with curl
?) how you got this JSON in the first place, then xidel
help out as well. No need to save it to a temporary file.
答案2
得分: 0
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
rem 以下设置用于源目录和文件名,这些是我用于测试的名称,特意包含了包含空格的名称,以确保该过程可以使用此类名称正常工作。这些需要根据您的情况进行更改。
SET "sourcedir=u:\your files"
SET "filename1=%sourcedir%\q75438837.txt"
SET "words="
SET "snagnext="
FOR /f "usebackqdelims=" %%e IN ("%filename1%") DO (
SET "LINE=%%e"
FOR %%y IN ({ } [ ] :) DO SET "line=!line:%%y=,!"
FOR %%y IN (!line!) DO (
IF DEFINED snagnext SET "words=!words!,%%~y"
SET "snagnext="
IF "%%~y"=="keyword" SET "snagnext=Y"
)
)
SET "words=%words:~1%"
SET words
GOTO :EOF
英文:
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
rem The following settings for the source directory and filename are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files"
SET "filename1=%sourcedir%\q75438837.txt"
SET "words="
SET "snagnext="
FOR /f "usebackqdelims=" %%e IN ("%filename1%") DO (
SET "LINE=%%e"
FOR %%y IN ({ } [ ] :) DO SET "line=!line:%%y=,!"
FOR %%y IN (!line!) DO (
IF DEFINED snagnext SET "words=!words!,%%~y"
SET "snagnext="
IF "%%~y"=="keyword" SET "snagnext=Y"
)
)
SET "words=%words:~1%"
SET words
GOTO :EOF
Note that if the filename does not contain separators like spaces, then both usebackq
and the quotes around %filename1%
can be omitted.
Read each line; replace each colon and various brackets with commas. The result is a simple comma-separated list in line
. Some elements may be quoted.
Use a simple for
to iterate through all of the elements on each line. If the element stripped of quotes and requoted is "keyword"
then set the snagnext
flag to y
.
The next element to appear when snagnext
is defined gets accumulated into the words
list.
Tip: Use set "var=value"
for setting string values - this avoids problems caused by trailing spaces. Don't assign "
or a terminal backslash or Space. Build pathnames from the elements - counterintuitively, it is likely to make the process easier. If the syntax set var="value"
is used, then the quotes become part of the value assigned.
答案3
得分: 0
用PowerShell解析JSON,可以借助以下方法:
这是一个简单的一行[标签:批处理文件]示例:
@%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile ',,,"""' + $((Get-Content 'keywords.txt' -Raw | ConvertFrom-Json).keywords.keyword -join ',') + '"""'& Pause
英文:
For parsing JSON, use the help of PowerShell:
Here's a simple one line [tag:batch-file] example:
@%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile ',,,"""' + $((Get-Content 'keywords.txt' -Raw | ConvertFrom-Json).keywords.keyword -join ',') + '"""'& Pause
答案4
得分: 0
这种更简单的方法将多个转换步骤合并为较少的命令,因此更高效且运行更快:
@echo off
setlocal EnableDelayedExpansion
set "keywords="
for /F "delims=," %%a in (keywords.txt) do (
set "line=%%a"
for /F "tokens=2 delims: " %%b in ("!line:*[=") do set "keywords=!keywords!,%%~b"
)
echo "!keywords:~1!"
- 第一个
for /F "delims=,"
命令从第一个逗号到行尾消除,因此它只获取{"keywords":[{"keyword":"halloween"
或{"keyword":"pumpkin"
中的内容,保存在line
变量中。 "!line:*[=!"
部分从行首消除直到第一个[
,如果有的话,因此它仅处理第一行,例如{"keyword":"halloween"
,即其他行的值相同。for /F "tokens=2 delims: "
命令针对上一个替换仅获取关键字值,如"halloween"
或"pumpkin"
。- 最后,
%%~b
用于添加每个关键字并消除引号。
英文:
This simpler method combine several conversion steps in lesser commands, so it is more efficient and run faster:
@echo off
setlocal EnableDelayedExpansion
set "keywords="
for /F "delims=," %%a in (keywords.txt) do (
set "line=%%a"
for /F "tokens=2 delims=:" %%b in ("!line:*[=!") do set "keywords=!keywords!,%%~b"
)
echo "!keywords:~1!"
- The first
for /F "delims=,"
command eliminates from first comma to end of line, so it just gets{"keywords":[{"keyword":"halloween"
or{"keyword":"pumpkin"
inline
variable. - The
"!line:*[=!"
part eliminates from beginning of line until first[
, if any, so it process only the first line giving{"keyword":"halloween"
; that is, the same value of the rest of lines. - The
for /F "tokens=2 delims=:"
command over previous replacement get just the keyword value, like"halloween"
or"pumpkin"
. - Finally, the
%%~b
used to add each keyword eliminate the quotes.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论