相关疑难解决方法(0)

使用Powershell将文件的内容转换为可以使用JSON传输的字符串

如何将文本文件的内容转换为字符串,然后将此字符串插入JSON文件?

例如,如果文件包含:

this
is
a
sample
file
Run Code Online (Sandbox Code Playgroud)

该脚本将生成:

"this\r\nis\r\na\r\nsample\r\nfile"
Run Code Online (Sandbox Code Playgroud)

要插入JSON模板:

"something":"<insertPoint>"
Run Code Online (Sandbox Code Playgroud)

生产:

"something":"this\r\nis\r\na\r\nsample\r\nfile"
Run Code Online (Sandbox Code Playgroud)

我正在使用Powershell 5并设法加载文件,生成一些JSON并通过运行来插入它:

# get contents and convert to JSON
$contentToInsert = Get-Content $sourceFilePath -raw | ConvertTo-Json
# write in output file
(Get-Content $outputFile -Raw).replace('<insertPoint>', $contentToInsert) | Set-Content $outputFile
Run Code Online (Sandbox Code Playgroud)

但是,还添加了许多其他不需要的字段.

"something":"{
  "value":  "this\r\nis\r\na\r\nsample\r\nfile"
  "PSPath":  "C:\\src\\intro.md",
  "PSParentPath":  "C:\\src",
  "PSChildName":  "intro.md",
    etc...
Run Code Online (Sandbox Code Playgroud)

最后,我正在尝试通过JSON将小型富文本段发送到网页,但希望使用Markdown在本地编辑和存储它们.如果这没有意义,并且有更好的方式发送这些,那么请让我知道.

powershell json

3
推荐指数
1
解决办法
714
查看次数

标签 统计

json ×1

powershell ×1