【问题标题】:VB.NET Is it possible to export JSON data to a file in JSON format?VB.NET 是否可以将 JSON 数据导出为 JSON 格式的文件?
【发布时间】:2018-01-14 06:47:16
【问题描述】:

我想知道是否可以使用 GET 请求从服务器获取 JSON 文本,然后将该数据以 JSON 格式输出到本地文件中。

我似乎唯一能找到的就是这个电话:

File.WriteAllText(AgentWorkingDirectory & "\json.txt", JsonConvert.SerializeObject(return_message))`

这似乎只给了我一行无格式文本。

所以不要这样:

{
    "AlertingRules":[
        {  
            "RuleId":1,
            "Name":"Unprocessed Directory",
            "RuleConditions":[
                {  
                    "Name":"FileCount",
                    "FileName":"",
                    ...

我明白了:

{"AlertingRules":[{"RuleId":1,"Name":"Unprocessed Directory","RuleConditions":[{"Name":"FileCount","FileName":null,...

【问题讨论】:

  • 这种紧凑的格式很有意义,因为它可以节省磁盘空间。您仍然可以使用任何理解 JSON 的编辑器打开该文件,它会显示它的打印效果。
  • 好的,当我在 JSON 编辑器中打开文件时,它仍然没有格式化。
  • @Riples 如果您的内容已经是 JSON 格式并且格式正确,为什么不直接将其写入磁盘呢?
  • @Riples 这个关于从 notepad++ 重新格式化 JSON 的答案可能会有所帮助:stackoverflow.com/a/5083037/6638533...

标签: json vb.net


【解决方案1】:

将格式化参数添加到您的序列化调用中

File.WriteAllText(AgentWorkingDirectory & "\json.txt",JsonConvert.SerializeObject(return_message, Newtonsoft.Json.Formatting.Indented))

Json.NET Documentation

【讨论】:

  • 哦哇......真的不敢相信我错过了这个。非常感谢,完美的答案。
猜你喜欢
  • 2020-10-03
  • 1970-01-01
  • 2019-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-10
  • 2017-03-07
相关资源
最近更新 更多