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.

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.

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

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.

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.

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.

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.

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.
Join the conversation! Your thoughts help the community grow.
Sign in to leave a comment
It is the same account you read, post and publish with — and you will come straight back to this page.








MirceaPosted Dec 28, 2018, 1:45 PM
Nice job; could you explain how to get the distance using distance-matrix?
Rohan TyagiPosted Dec 10, 2018, 4:04 AM
Hi ali your RAR file is incomplete i think please update this file .
Shivanand BastePosted Oct 26, 2016, 1:49 AM
Nice article... its helpful