Microsoft实用程序以以下格式返回字符串:
“作者:first.last; name:rootconfiguration;版本:2.0.0; generationDate; generationDate:06/01/2022 13:18:18:10; generationHost; generationHost:server; server; server;“;”
我想将这些字符串转换为简单的对象。如果这是真正的JSON,我只会使用Convertfrom-Json。为了尽可能少地重新发明轮子,将其转换为对象的最直接方法是什么(用钥匙作者,名称,版本,GenerationDate,GenerationDate,GenerationHost,具有明显的值。
如果“您只需要通过咬咬一下字符串来磨碎”,那就是答案,我可以做到这一点,但似乎应该有一种更简单的方法!)“做您的事情,但是将半刚果作为新线处理,接受右侧的空间等。”
A Microsoft utility returns strings in the following format:
"Author: First.Last; Name: RootConfiguration; Version: 2.0.0; GenerationDate: 06/01/2022 13:18:10; GenerationHost: Server;"
I would like to convert those strings into simple objects. If this were true JSON, I'd just use ConvertFrom-JSON. To reinvent the wheel as little as possible, what's the most straightforward way to convert that into an object (with keys Author, Name, Version, GenerationDate, GenerationHost, with the obvious values. It's fine if the values are all treated as dumb strings.
If "you just have to grind it out by tokenizing the string bite by bite" is the answer, I can do that, but it seems there should be a simpler way, like if I could tell ConvertFrom-JSON (or even ConvertFrom-String!) "Do your thing, but process the semicolons as newlines, accept spaces on the right hand side, etc."
发布评论
评论(3)
[Hashtable]
实例,该输入顺序尚未保留,具有固有的无序条目:AS zett42 如果输入字符串中包含的
\
chars中的值(与密钥相对)指出,他们'为了保留这样的保留 - 请参阅下面的评论。ConvertFrom-StringData
, but note that input order of the entries isn't preserved, given that the latter returns a[hashtable]
instance with inherently unordered entries:As zett42 points out, if the values (as opposed to the keys) in the input string contained
\
chars., they'd need to be doubled in order to be retained as such - see his comment below.我通常不会回答没有编码尝试的问题,但是认为这可能会对他人有所帮助。鉴于定界符是半龙,我当时想先转换为CSV,但接下来必须担心标题。因此,我们可以使用定界符将结果分开并一次处理一个值:
为了使这项工作我们需要在第一个结肠上分开(:> )将其余的完整留下; INCASE在值中的其他内容就像您在
GenerationDate
属性中看到的那样。$ _ -split“:”,2
实现的。最后,其余的只是将标题分配给
pscustomObject
,并使用convertto-json
将结果转换为JSON。注意:我仅限于我的工作系统上的“严格语言模式”而不是
new-Object
。I usually don't answer questions that don't have a coding attempt but, figured this might help others. Given that the delimiter is a semicolon, I was thinking of converting to CSV first but, would have to worry about the header next. So, instead of converting to CSV, we can use the delimiter to split the results at that and process the values one at a time:
To make this work we need to split at just the first colon (
:
) leaving the rest intact; incase there's others in the value like you see in theGenerationDate
property.$_ -split ":",2
.Finally, the rest was just left to assign the header to and value to a
PSCustomObject
and conver the results to JSON usingConvertTo-Json
.Note: I am restricted to a "strict language mode" on my work system so it's best to use the type-accelarator of
[PSCustomObject]@{..}
to create the object, rather thanNew-Object
.补充现有有用的答案,这是另一个使用
regex.matches.matches(
)
()
,其中第一个捕获a 键 ,第二个捕获a <强>价值。Complementing the existing helpful answers, here is another one using the
Regex.Matches()
function:Output:
( )
, where the first one captures a key and the second one captures a value.