用JSON代表学生?

时间:2016-08-25 07:43:03

标签: asp.net json

需要知道我们如何代表下面给出的学生。这是在ASP.NET中开发的

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }            
    public Address Address {get;set;}
}

public class Address         
{
    public int StreetNumber {get;set;}
    public string StreetName {get;set;}
    public string Suburb {get;set;}    
}

List<Person> students = new List<Person>();
students.Add(new Person { Name = "John", Age = 12, 
Address = new Address { StreetNumber = 9, StreetName = "Dominion Road", 
Suburb = "Mt Eden" }});

students.Add(new Person { Name = "Mary", Age = 13, 
Address = new Address { StreetNumber = 19, StreetName = "Queen Street", 
Suburb = "City" }});

1 个答案:

答案 0 :(得分:3)

以下是序列化的students

 [  
       {  
          "Name":"John",
          "Age":12,
          "Address":{  
             "StreetNumber":9,
             "StreetName":"Dominion Road",
             "Suburb":"Mt Eden"
          }
       },
       {  
          "Name":"Mary",
          "Age":13,
          "Address":{  
             "StreetNumber":19,
             "StreetName":"Queen Street",
             "Suburb":"City"
          }
       }
    ]

您可以使用Json.NET

等库来获取此信息
var json = JsonConvert.SerializeObject(students);