This article shows the usage of json operation class in .Net, which is a very important skill for .Net programming. The specific analysis is as follows:
First, you need to reference this library
The namespace needs to be added:
The data structure is as follows:
public class Team { public string Name { get; set; } public List<User> Users { get; set; } } public class User { public int Id { get; set; } public string Name { get; set; } }
Class --> JSON conversion code is as follows:
JavaScriptSerializer js = new JavaScriptSerializer(); Team team = new Team() { Name = "Dream Team", Users = new List<User>() { new User() { Id = 1, Name = "XXX" }, new User() { Id = 2, Name = "YYY" }, new User() { Id = 3, Name = "ZZZ" }, } }; string json = (team); (json);
Json --> Class conversion code is as follows:
string json = "{\"Name\":\"Dream Team",\"Users\":[{\"Id\":1,\"Name\":\"XXX\"},{\"Id\":2,\"Name\":\"YYY\"},{\"Id\":3,\"Name\":\"ZZZ\"}]}"; JavaScriptSerializer js = new JavaScriptSerializer(); Team team = <Team>(json);
I hope that the examples described in this article can help you learn from and help you.