JSON序列化以特定格式创建文本文件

时间:2015-12-11 23:36:39

标签: c# json file text format

我正在努力学习更多有关JSON的知识,以便构建一个读取文件中值的文件。 我正在使用C#并根据各种帖子和文章决定最终使用newsoft.JSON来实现这一目标。

以下是我希望该文件具有的格式:

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

我设法创建了部分文件,到目前为止这是我的代码:

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

我希望得到一些帮助就是“嵌套”#39; ,例如以&#34;&#34;&#34;&#34;&#34; {}&#34;开始 和嵌套区域,如应用程序,然后在deploy.net下面。 我正在努力让我的代码能够按照上面的示例生成嵌套。 任何指针和协助将不胜感激。 一旦我理解了如何获得

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

部分工作我应该能够管理其余部分。

我期待任何回复

确定更新: - ) 13-12-2015

再次感谢@Eser和@Plutonix。

我已经成功地获得了一些更多的结构(我希望......!因为JSON对我而言是新的,而且在c#中也相当新)

这是我的更新代码:

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

    }

我遇到的问题是我似乎无法弄清楚如何在文件中创建此格式:

"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"
    },

我可以设法将每个类的数据发送到这样的文件中:

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

将application1结构保存到文件中。&#39;

我无法正确嵌套,如下所示:-(

"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

我错过了什么? 这是我使用变量的方式吗?我应该使用数组还是结构?

0 个答案:

没有答案