0

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());
2
  • 1
    There's only one id on the top level, so ticketSearch["id"] will return 123456 I suppose. Commented Jul 1, 2020 at 21:14
  • id is single property, there is no need to cast it to list. Also, you are displaying the id of newly created instance of TicketInfo, which is 0. Make sense to assign a value Commented Jul 1, 2020 at 21:15

1 Answer 1

1

Ultimately you're ignoring the JSON and just outputting the default value of a manually-created instance:

var ticketInfo = new TicketInfo();
MessageBox.Show(ticketInfo.id.ToString());

Which will always be 0.

It looks like your code is drastically over-thinking the JSON deserialization process. (What is a SearchResult in your code, for example?) If you're using Newtonsoft's JSON.Net then they have documentation to help with this. Your entire process can simply be:

var ticketInfo = JsonConvert.DeserializeObject<TicketInfo>(response.Content);
MessageBox.Show(ticketInfo.id.ToString());
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for the pointer. I am using Newtonsoft's JSON.net but I'm still new to this side of C# and was following this page - mainly as I'll potentially be looking to expand on this and grab another two or three values form the returned JSON. However your above example has worked and I can ow return the ID. :)
@Rawns: Interesting, I hadn’t seen that example. I doubt the need for partial reading/deserialization will come up unless you have a really large JSON document though. (Like, megabytes in size.) For most cases, transferring the data will become the bottleneck before deserializing it does. In most cases simply deserializing the whole thing as above and just defining the fields you care about in the model will do the job.
Thanks for the clarity - much appreciated. How would I go about referencing one of the nested values in the JSON? Say for example I wanted the top level ID (which is now retrieved), but also wanted to extract say the Name value within the Status section of the JSON?
@Rawns: Your TicketInfo object would need a status property, which is an object that has a name property. Basically you can build up the model structure to match the JSON for the fields you want. I think there are even tools which will generate C# classes based on specified JSON, so that might be worth a Google search. You’d paste in the entire JSON and it’ll generate a class structure for it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.