如何使用Powershell将JSON对象保存到文件? [英] How to save a JSON object to a file using Powershell?

查看:297
本文介绍了如何使用Powershell将JSON对象保存到文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将以下JSON文件转换为Powershell表示对象.

I have converted the following JSON file to powershell representation object.

{
"computer": [
    {
        "children": [   
            {   
                "children": [ {
                   "children": [ {
                        "path": "T:\Dropbox\kvaki.html",
                        "name": "kvaki",
                        "type": "url",
                        "url": "http://example.com"
                   } ],
                    "path": "T:\Dropbox\",
                    "name": "Njusha",
                    "type": "folder"
                }, {
                    "path": "T:\Dropbox\Europa.html",
                    "name": "Europa",
                    "type": "url",
                    "url": "http://example.com"
                }, {
                    "path": "T:\Dropbox\math.html",
                    "name": "math",
                    "type": "url",
                    "url": "http://example.com"
                } ],
                "path": "T:\Dropbox\",
                "name": "Money",
                "type": "folder"
            }
        ],
        "full_path_on_file_sys": "T:\Dropbox\"
    }
]

}

在使用powershell表示进行了一些计算之后,我想将其保存为JSON文件. 但是命令$jsonRepresentation | ConvertTo-Json | Out-File "D:\dummy_path\file.json"是以这种方式保存的

After doing some computations with powershell representation I would like to save it to file as JSON. But command $jsonRepresentation | ConvertTo-Json | Out-File "D:\dummy_path\file.json" saves it in this way

{
    "computer":  [
                     {
                         "children":  " ",
                         "full_path_on_file_sys":  "T:\Dropbox\"
                     }
                 ]
}

问题:如何正确保存复杂的powershell JSON表示形式?

推荐答案

-ConvertTo-Json的深度参数解决了该问题.

-depth argument for ConvertTo-Json solves the issue.

$jsonRepresentation | ConvertTo-Json -depth 100 | Out-File "D:\dummy_path\file.json"

这篇关于如何使用Powershell将JSON对象保存到文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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