【问题标题】:How to put data in Following JSON Format using C#?如何使用 C# 将数据放入以下 ​​JSON 格式?
【发布时间】:2020-03-20 10:34:46
【问题描述】:

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

我已经走了这么远来获取this Answer 之后的缺失更新。

现在我需要使用以下格式将所有获取的数据放入 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# 数据设为这种格式?

谢谢

【问题讨论】:

  • 搜索jsonserialization
  • PatchType 和 PatchName 不需要用引号括起来吗?
  • 不同平台/版本的答案可能不同。你在开发什么/为了什么?框架 ?核 ?如果是 core , pre-3.0 ?
  • 编辑了我的问题。 @Jazb 我正在使用 string json = JsonConvert.SerializeObject(host); 将最终结果转换为 JSON 文件。但是如何将这些值放在我的 Model 中?

标签: c# json


【解决方案1】:

您可以使用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:请参阅 here 以获取示例。

【讨论】:

    【解决方案2】:
    1. 安装 Newtonsoft.Json 包并序列化您的对象。
    2. 遵循以下代码示例。
    List<Host> dataList=new List<Host>();
    string jsonString = JsonConvert.SerializeObject(dataList);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多