Get States based on pincode using c#



In this article, we will see how to get state information by using pincode api c#.

Here, first we need to make api call to pincode api by passing pincode of the area. 

The response will be the success objec with state information in json string.

Below is the code will get state information for passed pincode.

        public string GetStateByPincode(string pincode)
        {
            string stateName = "";
            try
            {
                if (!string.IsNullOrEmpty(pincode) && !string.IsNullOrWhiteSpace(pincode))
                {
                    //api call
                    var apiCall = string.Format("http://postalpincode.in/api/pincode/" + pincode);
                    var apiResult = new System.Net.WebClient().DownloadString(apiCall);
                    JavaScriptSerializer jsobject = new JavaScriptSerializer();
                    var result = jsobject.Deserialize<dynamic>(apiResult);
                    if (result["Status"] == "Success")
                    {
                        foreach (var item in result["PostOffice"])
                        {
                            stateName = item["State"];
                            break;
                        }
                    }
                }
                return stateName;
            }
            catch (Exception ex)
            {
                //log exception here
            }           
        }

 

Labels: postalpincode, postalpincode c# api, postalpincode api, get state using pincode api, state by pincode c#



Subscribe

Receive Quality Tutorials Straight in your Inbox by submitting your Email below:

Delivered by FeedBurner

Protected by Copyscape Duplicate Content Checker