在Powershell 3中美化JSON [英] Prettify json in powershell 3

查看:104
本文介绍了在Powershell 3中美化JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出标准的json字符串值:

Given a standard json string value:

$jsonString = '{ "baz": "quuz", "cow": [ "moo", "cud" ], "foo": "bar" }'

如何用换行符使它们变得漂亮,最好不要使用强力正则表达式?

How can I get this to be all pretty with newlines, preferably without brute-force regex?

到目前为止,我发现的最简单的方法是:

Simplest method I've found so far is:

$jsonString | ConvertFrom-Json | ConvertTo-Json 

但是,这似乎有点愚蠢.

However, that seems kinda silly.

推荐答案

为我工作.括号确保在管道传递之前已完成get-content.默认的convertto-json深度为2,通常太低.

Works for me. Parentheses make sure get-content is done before piping. Default depth of convertto-json is 2, which is often too low.

function pjson ($jsonfile) {
  (get-content $jsonfile) | convertfrom-json | convertto-json -depth 100 | 
    set-content $jsonfile
}

这篇关于在Powershell 3中美化JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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