2016-09-27 34 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