Reputation: 607
I've checked the response, this gives me back the information I need (which is not null) and gives me a response of 200. Whenever I try to deserialize the JSON returned, it shows null even though there should be information given.
This only happens when I try to grab one project, when I grab multiple this all works. I've tried to remove the ProjectList
class and tried to grab the information directly from the Project
class but this also returns nothing.
var client = new RestClient("website goes here");
var request = new RestRequest($"/api/v2/projects/{Global.PSlug}");
request.Method = Method.GET;
request.AddHeader("Accept", "application/json");
client.AddDefaultHeader("Authorization", string.Format("Bearer {0}", Global.ApiToken));
var response = client.Execute(request);
var deserialize = new JsonDeserializer();
ProjectList output = deserialize.Deserialize<ProjectList>(response);
HttpStatusCode statusCode = response.StatusCode;
int numericStatusCode = (int)statusCode;
if (numericStatusCode == 200)
{
MessageBox.Show(response.Content.ToString()); // gives info
output.Projects[0].Title = Global.PTitle;
output.Projects[0].Title = Global.PDescription;
ProjectTitle.Text = Global.PTitle;
ProjectDesc.Text = Global.PDescription;
}
Classes for deserializing
class ProjectList
{
public List<Project> Projects { get; set; }
}
class Project
{
public int Id { get; set; }
public string Title { get; set; }
public string Slug { get; set; }
public string Description { get; set; }
public string Deadline { get; set; }
public string User_Id { get; set; }
public string Created_At { get; set; }
public string Updated_At { get; set; }
}
Response is:
{
"Message": "OK",
"Project": {
"id": 13,
"title": "Api",
"slug": "apitest",
"description": "Api",
"deadline": "2020-03-01 00:00:00",
"user_id": 1,
"created_at": "2020-02-28 14:53:27",
"updated_at": "2020-02-28 14:53:27"
}
}
Upvotes: 0
Views: 266
Reputation: 2281
This is because the JSON you are getting and the Class you are using for deserialization are different. The JSON would require something like below.
class ApiResponse
{
public string Message { get; set; }
public Project Project { get; set; }
}
class Project
{
public int Id { get; set; }
public string Title { get; set; }
public string Slug { get; set; }
public string Description { get; set; }
public string Deadline { get; set; }
public string User_Id { get; set; }
public string Created_At { get; set; }
public string Updated_At { get; set; }
}
Upvotes: 2