Consuming json API in C# application – part 3

By | 8. March 2023

To fetch JSON API response from web and deserialize it to class in C# (NET 6 / 7), next steps are needed:

– Use the HttpClient class to send HTTP requests and receive HTTP responses³.
– Use the JsonSerializer class to deserialize JSON into objects²³.
– Create a class with properties that match the JSON properties you want to deserialize².

Class we already created in previous post Consuming json API in C# application

 

Here is an example of fetching JSON data response from public web API  deserializing it to a ApiResponse class:

It has two methods one for asynchronous and another for synchronous call.

Input parameter is page number to fetch, default is page 1.

Method returns ApiResponse class.

See also previous post Consuming json API in C# application – part 2

———————————————————————

(2) How to serialize and deserialize JSON using C# – .NET.
(3) json.net – C# Deserialize json api response – Stack Overflow.

Leave a Reply