使用Powershell将文件的内容转换为可以使用JSON传输的字符串 [英] Using Powershell to convert a file's contents into a string that can be transferred using JSON

查看:393
本文介绍了使用Powershell将文件的内容转换为可以使用JSON传输的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How does one convert a text file's contents into a string and then insert this string into a JSON file?

例如,如果文件包含:

this
is
a
sample
file

脚本将生成:

"this\r\nis\r\na\r\nsample\r\nfile"

要插入JSON模板,请执行以下操作:

To insert into a JSON template:

"something":"<insertPoint>"

产生:

"something":"this\r\nis\r\na\r\nsample\r\nfile"

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

I'm using Powershell 5 and have managed to load the file, generate some JSON and insert it by running:

# 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

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

However, a lot of other, unwanted fields are also added.

"something":"{
  "value":  "this\r\nis\r\na\r\nsample\r\nfile"
  "PSPath":  "C:\\src\\intro.md",
  "PSParentPath":  "C:\\src",
  "PSChildName":  "intro.md",
    etc...

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

Ultimately, I'm trying to send small rich text segments to a web page via JSON but want to edit and store them locally using Markdown. If this doesn't make sense and there's a better way of sending these then please let me know also.

推荐答案

iRon的答案建议不要使用字符串操作以在PowerShell中创建JSON,但使用哈希表(或自定义对象)来构造数据,然后然后将其转换为JSON.

iRon's answer helpfully suggests not using string manipulation to create JSON in PowerShell, but to use hashtables (or custom objects) to construct the data and then convert it to JSON.

但是,仅凭这些 并不能解决您的问题:

However, that alone does not solve your problem:

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-ContentNoteProperty属性的形式用元数据装饰其输出的字符串,而ConvertTo-Json当前总是 包括这些.

The root cause of the problem is that Get-Content decorates the strings it outputs with metadata in the form of NoteProperty properties, and ConvertTo-Json currently invariably includes these.

  • A proposal to allow opting out of this decoration when calling Get-Content can be found in this GitHub issue.
  • Complementarily, this GitHub issue suggests that ConvertTo-Json should ignore the extra properties for primitive .NET types such as strings.

最简单的解决方法是使用.psobject.baseobject 访问基础.NET实例,该实例绕过PowerShell用于提供额外属性的不可见包装对象:

The simplest workaround is to access the underlying .NET instance with .psobject.baseobject, which bypasses the invisible wrapper object PowerShell uses to supply the extra properties:

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

这篇关于使用Powershell将文件的内容转换为可以使用JSON传输的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆