简体   繁体   中英

How to put data in Following JSON Format using C#?

I have a task to fetch the list of all missing updates using a C# .NET Framwork 4.7.2 windows Service which runs at specific intervals.

I have gotten this far to Fetch the Missing updates following this Answe r.

Now I need to Put all the Fetched data in a JSON file using the following Format:

{
            "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"
                                  }
                        ]
                }
            ]
        }

Following is my C# Model:

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; }
}
}

My Question is How to put my C# Data in this Format?

Thanks

You could use Json.NET to achieve this, here is a possible implementation:

First you need to install the package using NuGet:

Install-Package Newtonsoft.Json -Version 12.0.3

Then you define your classes in the same way you are doing, you can, however, use C# naming conventions and make use of attributes to specify different names for serialization:

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; }
}

To serialize your class to json you can use the following statement:

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

To deserialize a json string back to the a C# object you use the opposite statement:

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

The code above should work for most cases but if you need to serialize / deserialize any of your objects in a special way you can write a custom JsonConverter: see here for an example.

  1. Install Newtonsoft.Json package and do serialization of your object.
  2. follow below code example.
List<Host> dataList=new List<Host>();
string jsonString = JsonConvert.SerializeObject(dataList);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM