0

I am busy trying to learn more about and understand JSON in order to build a file which reads the values in the file. I am using C# and based on various posts and articles decided to eventually use newsoft.JSON to get this achieved.

Here is the format I would like the file to have :

{
"__version": "2.3",
"__comments": "a sample output file created in c:\foo\",
"target.server": {
    "application": {
        "deploy.net": "network",
        "deploy.opt": "aggressive",
        "name": "aggressive-app",
        "type.disk.mode": true
    },
    "host": {
        "hostname": "192.168.1.32",
        "username": "root",
        "password": "host123456",
        "volume": "mydrive"
    },
    "network": {
        "hostname": "host1.abc.com",
        "dns.servers": [
            "192.168.1.253",
            "192.168.1.199"
        ],
        "gateway": "192.168.1.1",
        "ip": "192.168.1.200",
        "ip.family": "ipv4",
        "ipmode": "static",
        "mask": "24"
    },
    "os": {
        "password": "os12345678",
        "ssh.enable": true
    },
    "sso": {
        "password": "sso12345678",
        "domain-name": "abc.com",
        "site-name": "my-site"
    }
}}

I have managed to create part of the file and this is my code so far :

        public class Account
    {
        public DateTime CreatedDate { get; set; }
        public string __version { get; set; }
        public string __comments { get; set; }
        public string vista { get; set; }

    }

Account account = new Account
        {
            __version = "2.3",
            __comments = @"a sample output file created in c:\foo",
            CreatedDate = new DateTime(2015, 12, 22, 0, 0, 0, DateTimeKind.Utc),
            Roles = new List<string>
{
    "User",
    "deploy.net",
    "Admin"
}
        };

Where I would like some help is just with the 'nesting' , for example starting with ""target.server"{ }" and the nested areas like application and then below that deploy.net. I am just battling to get my code to be able to produce the nesting as per the sample above. Any pointers and assistance would be appreciated. Once I understand how how to get the

"target.server":{"application":{"deploy.net"":"network"}} 

portion working I should be able to manage the rest.

I look forward to any replies

Ok..an UPDATE :-) 13-12-2015

Once again thanks @Eser and @Plutonix.

I have managed to at least get some more structure (I hope.. ! as JSON isuite new to me and fairly new at c# too)

Here 's my updated code :

 public partial class test
    {

        [JsonProperty("__version")]
        public string Version { get; set; }

        [JsonProperty("__comments")]
        public string Comments { get; set; }

        [JsonProperty("target.server")]
        public TargetServer TargetServer { get; set; }
    }

    public class TargetServer
    {

        [JsonProperty("application")]
        public Application2 Application { get; set; }

        [JsonProperty("host")]
        public Host2 Host { get; set; }

        [JsonProperty("network")]
        public Network2 Network { get; set; }

        [JsonProperty("os")]
        public Os2 Os { get; set; }

        [JsonProperty("sso")]
        public Sso2 Sso { get; set; }
    }

    public class Sso2
    {

        [JsonProperty("password")]
        public string Password { get; set; }

        [JsonProperty("domain-name")]
        public string DomainName { get; set; }

        [JsonProperty("site-name")]
        public string SiteName { get; set; }
    }

    public class Os2
    {

        [JsonProperty("password")]
        public string Password { get; set; }

        [JsonProperty("ssh.enable")]
        public bool SshEnable { get; set; }
    }

    public class Network2
    {

        [JsonProperty("hostname")]
        public string Hostname { get; set; }

        [JsonProperty("dns.servers")]
        public string[] DnsServers { get; set; }

        [JsonProperty("gateway")]
        public string Gateway { get; set; }

        [JsonProperty("ip")]
        public string Ip { get; set; }

        [JsonProperty("ip.family")]
        public string IpFamily { get; set; }

        [JsonProperty("ipmode")]
        public string Ipmode { get; set; }

        [JsonProperty("mask")]
        public string Mask { get; set; }
    }

    public class Host2
    {

        [JsonProperty("hostname")]
        public string Hostname { get; set; }

        [JsonProperty("username")]
        public string Username { get; set; }

        [JsonProperty("password")]
        public string Password { get; set; }

        [JsonProperty("volume")]
        public string Volume { get; set; }
    }

    public class Application2
    {

        [JsonProperty("deploy.net")]
        public string DeployNet { get; set; }

        [JsonProperty("deploy.opt")]
        public string DeployOpt { get; set; }

        [JsonProperty("name")]
        public string Name { get; set; }

        [JsonProperty("type.disk.mode")]
        public bool TypeDiskMode { get; set; }
    }

 private void button1_Click(object sender, EventArgs e)
    {
        test Test = new test
        {

            Comments = "a sample output file created in c:\foo",
            Version = "2.3",

        };
        string json = JsonConvert.SerializeObject(Test, Formatting.Indented);
        System.IO.File.WriteAllText(@"c:\foo\newjson6.txt", json);
        System.Diagnostics.Debug.WriteLine(json);

    }

The problem I have is I can't seem to figure out how to create this format in the file :

"target.server": {
    "application": {
        "deploy.net": "network",
        "deploy.opt": "aggressive",
        "name": "aggressive-app",
        "type.disk.mode": true
    },
    "host": {
        "hostname": "192.168.1.32",
        "username": "root",
        "password": "host123456",
        "volume": "mydrive"
    },

I can manage to send the data from each class into a file like this :

Application2 application1 = new Application2
        {
            Deploynet = "network",
            Deployopt = "aggressive",
            Name = "agressive-app",
           ThinDiskMode = true

         };
        string json = JsonConvert.SerializeObject(application1, Formatting.Indented);
        System.IO.File.WriteAllText(@"c:\foo\newjson6.txt", json);
        System.Diagnostics.Debug.WriteLine(json);

Which would save the application1 structure to a file.'

I just can't get the nesting right as below :-(

"target.server": {
    "application": {
        "deploy.net": "network",
        "deploy.opt": "aggressive",
        "name": "aggressive-app",
        "type.disk.mode": true
    }, //.....Etc for Host and the rest of the classes

What am I missing ? Is it the way I am using a variable ? Should I use an array or struct ?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
user3407537
  • 51
  • 1
  • 13
  • Read about `JsonProperty` attribute. – Eser Dec 11 '15 at 23:39
  • Thanks a million Eser will do it again ...have been reading frantically to get up to speed ;-) sore after-midnight eyes now. – user3407537 Dec 11 '15 at 23:44
  • @Eser I had a quick search and I think it's what I need ..found [link]http://stackoverflow.com/questions/12583638/when-is-the-jsonproperty-property-used-and-what-is-it-used-for but whenever I try this code'//example of json that is submitted "Car":{ "Type":"Ferrari", } //where it gets mapped public static class Car { @JsonProperty("Type") public String type; }' - I get method must have a return type .. can you possibly shed some light. – user3407537 Dec 12 '15 at 00:11

0 Answers0