System.Web.Extensions.dll中类JavaScriptSerializer可以帮助我们把C#对象转化为JSON字符串。
有一个Person类
1 public class Person 2 { 3 private string name; 4 5 public string Name 6 { 7 get { return name; } 8 set { name = value; } 9 } 10 private int age; 11 12 public int Age 13 { 14 get { return age; } 15 set { age = value; } 16 } 17 }
将List<Person>转化为JSON字符串
1 List<Person> persons = new List<Person>(); 2 persons.Add(new Person() {Name="zhangsan" ,Age=26}); 3 persons.Add(new Person() { Name = "lisi", Age = 26 }); 4 JavaScriptSerializer jss = new JavaScriptSerializer(); 5 string personsjson = jss.Serialize(persons);
转化JSON字符串
personsjson = "[{"Name":"zhangsan","Age":26},{"Name":"lisi","Age":26}]"
时间: 2024-10-06 02:21:47