Best Way to Consume JSON from REST API in .NET

consume-restful

You can consume an API in two ways. One is to do it synchronously and the other asynchronously. It is very important to consume APIs and web services. You can create online endpoints using the service oriented architecture so different clients may be able to access it across the web. We communicate many if not most of the digital systems through the Application programming interface (API).

Use of REST

The importance of web APIs is increasing because it gives direct communication lines between the client and the customer for a wide range of applications. Such distributed systems use Representation State Transfer (REST) design models. REST principles set the rules for the design of the web APIs. This results in a simpler and more general web service that is saleable and easier to consume.

We had many powerful service oriented frameworks and technologies that people used though they were not always easy to use. These included the traditional ASP.NET services, XML-based services, WCF services, and SOAP based services. One of the best web based architecture is .NET that has come a long way. One of the reasons is that RESTful services have become predominant model design. This makes consuming and exposing RESTful services easier. But, we cannot say the same of the .Net world.

So, to make it clear, easy and concrete, lets build something right away. We will start with a simple console application project. Fire up Visual Studio (I’m using VS 2012), and create a new console application project.

Consuming JSON REST

Update the new version

When we get to use a new version of the .NET framework and features, we must revise it so we can implement it in a new and easier way. There are many opinions and discussions, techniques and samples on how to sample the .NET applications. To understand it more clearly, the idea is to build something first. This console application project is a simple weather API client. It uses the Open WeatherMap.org JSON API. Through this API, we can locate a city and get the weather forecast for it.

We will implement a simple Weather API client by using the Open WeatherMap.org JSON API. The API will allow us to locate a city location and get the weather forecast information.Lets create a new class to manage the API client logic. Right click on the project, select Add->Class. Name it WeatherApiClient.cs.

WeatherApiClient

To manage the API client logic, we create a new class. Click on the project and click on AddàClass. Name this as WeatherApiClient.cs. To keep it simple, we make the class static so you can call methods straightaway from the main program class. We first implement the synchronous client method. Create a new void method in the class and call it GetWeatherForecast (). Again, make this static.

GetWeatherForecast

Two options for methods

We need to use a method to consume the API. You can use two different classes, the WebClient or the HttpWebRequest. In general, we see that the WebClient offers a simpler and easier implementation. The HttpWebRequest allows more control over the execution of the requests. Here we use the WebClient and uses a method called DownloadString (). This method receives a string URL and sends back a response string having the JSON response. You have to add a reference to System.Net in the project to use WebClient.

Here is the main code you need:
public static class WeatherApiClient
{
	public static void GetWeatherForecast()
	{
		var url = "http://api.openweathermap.org/data/2.1/find/citylat=51.50853&lon=-0.12574&cnt=10";
  
		// Synchronous Consumption
		var syncClient = new WebClient();
		var content = syncClient.DownloadString(url);
 
		// Create the Json serializer and parse the response
		DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(WeatherData));
		using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(content)))
		{
			var weatherData = (WeatherData)serializer.ReadObject(ms);
		}
	}
}

For this, you declare a sample URL then create the WebClient variable. Call the method to pass the URL. Call the method and put a breakpoint to it to check the response from the API. View the JSON response through the Text Visualizer tool. This works straight and gives us the JSON response when we call the API.

Have Something in Mind? Let's Talk

Have a look at the services and development process of the iQlance solution. See What process we follow for mobile app and software development. Have a look at how we are praised by our clients Start a conversation to innovate your next great idea into reality with us.

How Can We Help?


    1 + 8 =

    Subscribe Our newsletter

    cluth
    goodfirms
    Google
    gesia
    iso
    nasscom
    startup
    itfirms
    ypca