Parsing JSON In C# Using Google Geocoding API

In this article, I am going to demonstrate how we will parse JSON in C#. JSON stands for JavaScript Object Notation and if you don’t know about JSON, please Google it and get an idea of what it really means. We are going to use Geocoding API that will return a response and then we will see how we can extract the required data by using such APIs. We are going to implement a system, which will take a name of the city from us and will return the coordinates of the city to us; i.e., latitude and longitude.

There is a need to know that every API returns data either in XML format or in JSON format. JSON is more popular nowadays. Hence, I am going to demonstrate this by creating a simple Windows form Application.

Step 1

Start your Visual Studio. Create a Windows form Application and name it, as per your desire.

Windows

Step 2

After creating the project, drag two labels, one textbox and a button and name it after lbl_latitude, lbl_longitude and txbx_City respectively.

Windows

Step 3

Now, double click the button and an event will be created in the code file at the backend.

Windows

Step 4

Now, open your Browser, copy and paste the link, mentioned below. You will see the result, mentioned below. I have scrolled down to show the coordinate’s information only. If you are wondering how well structured the JSON is being showed in the Browser, use JSON View Extension of Google Chrome. 

Windows

Step 5

Now, we are going to use this API in our Application. For JSON parsing, we have to map the output, which is shown in the Browser to the classes in our coding. Instead of doing manual hard work to create the classes and the respective fields, we have a well-automated tool, which performs this function on behalf of us. Go to Json2Csharp, copy and paste the link, which we opened in our Browser in the Input field and click the button, generated below. 

Windows

Copy all the classes one by one and create them in Visual Studio without changing anything. You can do this by creating separate classes or just by creating one class and paste all the classes there. See the image, given below.

Windows

Step 6

Now, open your NuGet Manager by going to Solution Explorer -> project-> references-> manage NuGet packages. Type the search bar newtonSoft.json. Go ahead and install the package, which will be helpful for us to do JSON parsing.

Windows

Step 7

After installation, go back to the code at the backend file of our form i.e. to the button event, which we have created in the start.

  • Instantiate HttpClient class.

    Windows

  • Get the string, which will be entered by the user and the URL, which will return the response. I have used String.Format method here to embed the user input in the URL.

    Windows

  • Call the GetAsync() method of HttpClient by passing the URL string in the parameters.

    Windows

  • Read the string from the response by calling the method, mentioned below.

    Windows

  • Call the Deserialize Method of the JsonConvert.

    Windows

  • Finally, loop through the results to get the latitude and the longitude of the entered city, as shown below. We can find all the attributes and their values through the OOP approach and can use them accordingly. As a result, it is returning an array, so we have to loop it through to find the remaining results.

    Windows

Step 8

Now, show the result in the labels.

Windows

We can apply all the validations required by us and mandatory for us. In a similar way, we can parse any JSON result, we want to. By following this tutorial, you can also use any API by considering the requirements of API. Most APIs require authentication to get the result.

Next Recommended Readings