我正在尝试使用我收到的json加载网格。这是我在Home.cs
中的代码:
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
是null,这可能是错的吗?
答案 0 :(得分:1)
尝试将Ticket类中的公共字段更改为属性:
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将始终为null(表达式尚未执行)。