2016-09-27 36 views
0

我试图加载与我收到的JSON网格。这是我在Home.cs代码:C#与json加载网格

private void getTickets() 
     { 
      try 
      { 
       string urlName = "tickets"; 
       string method = "GET"; 
       string json = null; 
       HttpStatusCode statusCode = HttpStatusCode.Forbidden; 
       Tickets data = null; 

       RestClient client = RequestClient.makeClient(); 
       MakeRequest request = new MakeRequest(null, null, urlName, method); 

       IRestResponse response = client.Execute(request.exec()); 

       statusCode = response.StatusCode; 
       json = response.Content; 
       var ticketWrapper = JsonConvert.DeserializeObject<TicketWrapper>(json); 

       if ((int)statusCode == 200) 
       { 
        gridTicket.DataSource = ticketWrapper.tickets; 
       } 

       else 
       { 
        MessageBox.Show("error"); 
       } 
      } 

      catch (Exception ex) 
      { 
       MessageBox.Show(ex.Message); 
      } 
     } 

票务包装类

class TicketWrapper 
    { 
     public IEnumerable<Tickets> tickets { get; set; } 
    } 

门票类

class Tickets 
    { 
     public int id; 
     public string title; 
     public string description; 
     public int user_id; 
     public int subject_id; 
    } 

如果我调试我可以看到,我收到了JSON,但ticketWrapper是空的什么可能是错误的?

调试图像:enter image description here

+0

你可以添加收到的json字符串到你的问题? – croxy

回答

1

尝试更改票务类公共领域性质:

class Tickets 
{ 
    public int id { get; set; } 
    public string title { get; set; } 
    public string description { get; set; } 
    public int user_id { get; set; } 
    public int subject_id { get; set; } 
} 

我同时认为IEnumerable不能序列化的最佳选择。尝试在TicketWrapper中使用List。

此外,将您的断点向下移动,因为在当前位置中ticketWrapper将始终为空(表达式尚未执行)。

+0

Thankyou工作很棒! – Jamie