首页 > 解决方案 > 如何使用 C# 将数据放入以下 ​​JSON 格式?

问题描述

我的任务是使用 .NET Framwork 4.7.2以特定时间间隔运行的 C# windows 服务获取所有丢失更新的列表。

在这个答案之后,我已经走了这么远来获取缺失的更新。

现在我需要使用以下格式将所有 Fetched 数据放入 JSON 文件中:

{
            "hostName": "LRD-SomeHost",
            "ip" : "192.168.13.12",
            "mac" : "MAC:23:23:123:AS"
            "timeStamp" : "CURRENT_TIME_STAMP",
            "updates" : [
                {
                    "updateID": "b32e464f-2e4a-4109-9018-33583a079a8a",
                    "updateDetails": [
                                  {
                                    "patchDescription" : "Some Long Description",
                                    "patchCategory" : "28bc880e-0592-4cbf-8f95-c79b17911d5f"
                                    "patchType" : "UpdateClassification"
                                    "patchName" : "Update Rollups"
                                  },
                                  {
                                    "patchDescription" : "Windows 10"
                                    "patchCategory" : "a3c2375d-0c8a-42f9-bce0-28333e198407"
                                    "patchType" : "Product"
                                    "patchName" : "Windows 10"
                                  },
                                  {
                                    "patchDescription" : "Windows 10 LTSB"
                                    "patchCategory" : "d2085b71-5f1f-43a9-880d-ed159016d5c6"
                                    "patchType" : "Product"
                                    "patchName" : "Windows 10 LTSB"
                                  }
                        ]
                }
            ]
        }

以下是我的 C# 模型:

namespace UpdateCollector
{
 public class Host
{
    public string hostname { get; set; }
    public string ip { get; set; }
    public string mac { get; set; }
    public DateTime? timeStamp { get; set; }
    public List<Updates> updates { get; set; }

}

public class Updates
{
    public string updateID { get; set; }
    public List<UpdateDetails> updateDetails { get; set; }
}

public class UpdateDetails
{
    public string patchDescription { get; set; }
    public string patchCategory { get; set; }
    public string patchType { get; set; }
    public string patchName { get; set; }
}
}

我的问题是如何以这种格式放置我的 C# 数据?

谢谢

标签: c#json

解决方案


您可以使用Json.NET来实现这一点,这是一个可能的实现:

首先,您需要使用 NuGet 安装包:

Install-Package Newtonsoft.Json -Version 12.0.3

然后您以与您相同的方式定义您的类,但是,您可以使用 C# 命名约定并使用属性来指定不同的序列化名称:

public class Host
{
    [JsonProperty(PropertyName = "hostname")]
    public string Hostname { get; set; }
    [JsonProperty(PropertyName = "ip")]
    public string Ip { get; set; }
    [JsonProperty(PropertyName = "mac")]
    public string Mac { get; set; }
    [JsonProperty(PropertyName = "timeStamp")]
    public DateTime? TimeStamp { get; set; }
    [JsonProperty(PropertyName = "updates")]
    public List<Updates> Updates { get; set; }
}

public class Updates
{
    [JsonProperty(PropertyName = "updateID")]
    public string UpdateId { get; set; }
    [JsonProperty(PropertyName = "updateDetails")]
    public List<UpdateDetails> UpdateDetails { get; set; }
}

public class UpdateDetails
{
    [JsonProperty(PropertyName = "patchDescription")]
    public string PatchDescription { get; set; }
    [JsonProperty(PropertyName = "patchCategory")]
    public string PatchCategory { get; set; }
    [JsonProperty(PropertyName = "patchType")]
    public string PatchType { get; set; }
    [JsonProperty(PropertyName = "patchName")]
    public string PatchName { get; set; }
}

要将您的类序列化为 json,您可以使用以下语句:

var host = new Host();
// Fill all the properties, lists etc...
string json = JsonConvert.SerializeObject(host, Formatting.Indented);

要将 json 字符串反序列化回 C# 对象,请使用相反的语句:

Host host = JsonConvert.DeserializeObject<Host>(json);

上面的代码应该适用于大多数情况,但如果您需要以特殊方式序列化/反序列化任何对象,您可以编写自定义 JsonConverter:请参阅此处以获取示例。


推荐阅读