I'm hooking into a REST API to retrieve data and the returned data is in JSON format like so:
{
"id": 123456,
"summary": "Summary Value",
"recordType": "ServiceTicket",
"board": {
"id": 22,
"name": "Service Desk",
"_info": {
"board_href": "https://xxxxxxxxxxxxxxxxx"
}
},
"status": {
"id": 453,
"name": "Assigned",
"_info": {
"status_href": "https://xxxxxxxxxxxxxxxxx"
}
},
"company": {
"id": 19381,
"identifier": "CompanyName",
"name": "Full Company Name",
"_info": {
"company_href": "https://xxxxxxxxxxxxxxxxx",
"mobileGuid": "111111-222222-333333-444444-5555555"
}
},
"site": {
"id": 1088,
"name": "Main",
"_info": {
"site_href": "https://xxxxxxxxxxxxxxxxx",
"mobileGuid": "111111-222222-333333-444444-5555555"
}
},
"siteName": "Main",
"addressLine1": "Street 1",
"addressLine2": "Street 2",
"city": "London",
.
.
.
.
The data returned comprises over 300 lines of various values and info but I'm only concerned with pulling out the very first id value (123456). I'm using Json .NET to try and do this and looked at many example codes including their documentation on deserializing partial data but when I output the value of id, all I get is 0.
My public class:
public class TicketInfo
{
public int id { get; set; }
}
The code to get the ID and output in a message box:
string json = response.Content; // The above JSON from a RestSharp query
JObject ticketSearch = JObject.Parse(json);
IList<JToken> results = ticketSearch["id"].ToList();
IList<SearchResult> searchResults = new List<SearchResult>();
foreach (JToken result in results)
{
SearchResult searchResult= result.ToObject<SearchResult>();
searchResults.Add(searchResult);
}
var ticketInfo = new TicketInfo();
MessageBox.Show(ticketInfo.id.ToString());
ticketSearch["id"]will return 123456 I suppose.idis single property, there is no need to cast it to list. Also, you are displaying theidof newly created instance ofTicketInfo, which is0. Make sense to assign a value