简体   繁体   English

序列化嵌套对象(json.net)

[英]Serialize a nested Object (json.net)

I use Json.Net to serialize my objects, but now I need to serialize an nested object 我使用Json.Net序列化我的对象,但是现在我需要序列化一个嵌套的对象

sample serialize an object: https://www.newtonsoft.com/json/help/html/SerializeObject.htm 示例序列化对象: https : //www.newtonsoft.com/json/help/html/SerializeObject.htm

public class Account
{
    public string Email { get; set; }
    public bool Active { get; set; }
    public DateTime CreatedDate { get; set; }
    public IList<string> Roles { get; set; }
}

Account account = new Account
{
Email = "james@example.com",
Active = true,
CreatedDate = new DateTime(2013, 1, 20, 0, 0, 0, DateTimeKind.Utc),
Roles = new List<string>
{
    "User",
    "Admin"
}
};

string json = JsonConvert.SerializeObject(account, Formatting.Indented);
 // {
 //   "Email": "james@example.com",
 //   "Active": true,
 //   "CreatedDate": "2013-01-20T00:00:00Z",
 //   "Roles": [
 //     "User",
 //     "Admin"
 //   ]
 // }

Console.WriteLine(json);

But now I need a Json like: 但是现在我需要一个像这样的Json:

 // {
 //   "Email": "james@example.com",
 //   "Active": true,
 //   "CreatedDate": "2013-01-20T00:00:00Z",
 //   "Roles": [
 //     "User"{
 //     "key": "value",
 //     "key": "value"
 //      }
 //     "Admin"
 //   ]
 // }

How can I build this Json? 我如何建立这个Json?

You need to create a separate/nested class as per your requirement. 您需要根据需要创建一个单独的/嵌套的类。 Please find below code and let me know if you require any additional information or change in it: 请找到以下代码,如果您需要任何其他信息或对其进行更改,请告诉我:

    private void Serialise()
    {

        //prepare static data
        List<User> users = new List<User>()
        {
            new User() {key = "value1"},
            new User() {key = "value2"}
        };

        Roles roles = new Roles();
        roles.Users = users;
        roles.Role = "Admin";

        Account account = new Account
        {
            Email = "james@example.com",
            Active = true,
            CreatedDate = new DateTime(2013, 1, 20, 0, 0, 0, DateTimeKind.Utc),
            Roles = roles
        };

        //serialise
        string json = JsonConvert.SerializeObject(account, Formatting.Indented);
    }

    public class Account
    {
        public string Email { get; set; }
        public bool Active { get; set; }
        public DateTime CreatedDate { get; set; }
        public Roles Roles { get; set; }
    }

    public class Roles
    {
        public List<User> Users { get; set; }
        public string Role { get; set; }
    }

    public class User
    {
        public string key { get; set; }
    }
public class Account
    {
        public string Email { get; set; }
        public bool Active { get; set; }
        public DateTime CreatedDate { get; set; }
        public IList<Roles> Roles { get; set; }
    }

    public class Roles
    {
        public Dictionary<string, string> User { get; set; }

    }

static void Main(string[] args)
        {
    Account account = new Account
            {
                Email = "james@example.com",
                Active = true,
                CreatedDate = new DateTime(2013, 1, 20, 0, 0, 0, DateTimeKind.Utc),
                Roles = new List<Roles> { new Roles { User=new Dictionary<string, string>
                                            {
                    {"Key","value" },
                    {"key","value" }
                                            }
                },

                },
            };

                string json=JsonConvert.SerializeObject(account,Newtonsoft.Json.Formatting.Indented);
            Console.WriteLine(json);
            Console.ReadLine();

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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