Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Powershell转换文件';将内容转换为可以使用JSON传输的字符串_Json_Powershell - Fatal编程技术网

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

使用Powershell转换文件';将内容转换为可以使用JSON传输的字符串,json,powershell,Json,Powershell,如何将文本文件的内容转换为字符串,然后将该字符串插入JSON文件 例如,如果文件包含: this is a sample file 脚本将生成: "this\r\nis\r\na\r\nsample\r\nfile" 要插入JSON模板,请执行以下操作: "something":"<insertPoint>" 我正在使用Powershell 5,并已通过运行以下命令加载文件、生成一些JSON并插入它: # get contents and convert to JSON $co

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

例如,如果文件包含:

this
is
a
sample
file
脚本将生成:

"this\r\nis\r\na\r\nsample\r\nfile"
要插入JSON模板,请执行以下操作:

"something":"<insertPoint>"
我正在使用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
最终,我尝试通过JSON将小的富文本段发送到web页面,但希望使用标记在本地编辑和存储它们。如果这没有意义,并且有更好的发送方式,请也让我知道。

除了@mklement0所描述的实际问题和添加到
获取内容
结果中的元数据之外,这只是一个一般性建议:

不要在任何
Json
内容中插入(替换、插入等)。
相反,在将对象转换为()一个
Json
文件之前,修改该对象(如有必要,使用以还原该对象)。
在本例中,我将使用a和a来表示:

@{'something' = @'
this
is
a
sample
file
'@
} | ConvertTo-Json
结果:

{
    "something":  "this\nis\na\nsample\nfile"
}

您可以使用
Out-String
cmdlet首先将
Get-Content
的输出强制为平面字符串:

@{ "something" = (Get-Content lines.txt | Out-String) } | ConvertTo-Json
这将产生:

{
    "something":  "this\r\nis\r\na\r\nsample\r\nfile\r\n"
}
有益的建议是,不要在PowerShell中使用字符串操作创建JSON,而是使用哈希表(或自定义对象)构造数据,然后将其转换为JSON

但是,仅此一点并不能解决您的问题:

PS> @{ something = Get-Content -Raw $sourceFilePath } | ConvertTo-Json
{
  "something": {
    "value": "this\nis\na\nsample\nfile\n",
    "PSPath": "/Users/mklement/Desktop/pg/lines.txt",
    # ... !! unwanted properties are still there
}
问题的根本原因是
Get Content
NoteProperty
属性的形式用元数据装饰它输出的字符串,而
ConvertTo Json
当前总是包含这些内容

  • 有关允许在调用
    Get Content
    时选择退出此装饰的建议,请参见
  • 作为补充,建议
    converttojson
    应该忽略原始.NET类型(如字符串)的额外属性
最简单的解决方法是使用
.psobject.baseobject
访问底层.NET实例,它绕过PowerShell用于提供额外属性的不可见包装器对象:

PS> @{ something = (Get-Content -Raw $sourceFilePath).psobject.baseobject } |
      ConvertTo-Json
{
  "something": "this\nis\na\nsample\nfile\n"
}

您的解决方案是有效的,但值得一解释:OP使用的
Get Content-Raw
已经保证了使用单行、多行字符串,
-Raw
通常是更简单、更快的方法(
Out string
的目的是格式化以显示任意命令输出的字符串表示)。这就是说,在这种情况下,使用
Out String
确实解决了OP的问题,因为通过从
Get Content
输出的字符串创建一个新字符串,而不使用后者添加的
NoteProperty
成员,
ConvertTo Json
会产生预期的序列化。注意:在这种情况下,这可能无关紧要,但通常值得注意的是,
获取内容
/
输出字符串
组合不会忠实地保留最后一个输入行的尾随换行或非尾随状态,并且总是附加最后一个换行<相反,代码>获取内容-原始
,没有这个问题。
PS> @{ something = Get-Content -Raw $sourceFilePath } | ConvertTo-Json
{
  "something": {
    "value": "this\nis\na\nsample\nfile\n",
    "PSPath": "/Users/mklement/Desktop/pg/lines.txt",
    # ... !! unwanted properties are still there
}
PS> @{ something = (Get-Content -Raw $sourceFilePath).psobject.baseobject } |
      ConvertTo-Json
{
  "something": "this\nis\na\nsample\nfile\n"
}