C# Store Locator Software Sample Code

This page will walk you through the task of creating a simple C# console program to call our REST search API.  It uses a Json reader with c#.

It is assumed that you have your client ID and API key, and that you are using Visual Studio 2010, with NuGet installed.

Note that this example is only showing one approach to accessing a REST web service, and deserializing JSON. There are several other ways this could be done.

  1. Create a new project, Visual C#, console application.
  2. Target framework should be 4.0 or later. (Note that 4.0 client profile will not work.)
  3. Using NuGet, add a reference to EasyHttp. (This will also pull in JsonFx.)
  4. Optionally, add a reference to Json.NET.
  5. Add the code shown below to Program.cs.
  6. Substitute in your own client ID and API key.
  7. Compile & run.

Note that we are deserializing the JSON to a dynamic variable. This way, you do not need to create a class that exactly mirrors our location result class. You can simply pull out the fields you need. This allows for maximum flexibility, and minimizes the chance that your code could break if we make API changes in the future.

We suggest pulling in Json.NET if you need to deserialize to a pre-defined class. We have had some issues doing that with JsonFx. Json.NET can also be helpful if you wish to use LINQ to JSON.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using EasyHttp.Http;
using JsonFx.Json;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace RestWebSvcCsharpConsoleTest
    class Program
        static void Main(string[] args)
            string myUrl = 
                + "?ClientId=999&ApiKey=YOUR-API-KEY-HERE";
            // presumably, these values would come from the end-user 
            // and/or your business logic:
            int countryId = 1;      // US
            string zipCode = "08876";
            string city = "";
            string state = "";      // e.g. "NJ"
            int radius = 25;
            bool returnGeocodes = true;
            bool returnAttributes = false;
            // construct the full URL:
            myUrl += string.Format("&CountryId={0}", countryId);
            if (!string.IsNullOrEmpty(zipCode))
                myUrl += string.Format("&PostalCode={0}", zipCode);
            if (!string.IsNullOrEmpty(city))
                myUrl += string.Format("&City={0}", city);
            if (!string.IsNullOrEmpty(state))
                myUrl += string.Format("&State={0}", state);
            myUrl += string.Format("&Radius={0}", radius);
            if (returnGeocodes)
                myUrl += "&ReturnGeocode=true";
            if (returnAttributes)
                myUrl += "&FillAttr=true";
            myUrl += string.Format("&StartIndex={0}&PageSize={1}", 0, 25);
            var http = new HttpClient
                Request = { Accept = HttpContentTypes.ApplicationJson }
            EasyHttp.Http.HttpResponse response = null;
                response = http.Get(myUrl);
            catch (WebException ex)
                Console.WriteLine("Error: web exception - msg = {0}", ex.Message);
            var statcode = response.StatusCode;
            string statdesc = response.StatusDescription;
            if (statcode != System.Net.HttpStatusCode.OK)
                Console.WriteLine("Error: response code = {0}", statdesc);
            // get the raw JSON:
            string rawresp = response.RawText;
            if (string.IsNullOrEmpty(rawresp))
                Console.WriteLine("Error: no response text received from server.");
            // create a dynamic var and pop it from the raw JSON:
            var reader = new JsonFx.Json.JsonReader();
            dynamic myResults = reader.Read(rawresp);
            int nTotResults = myResults.TotalResults;
            // optionally, deserialize it to a static variable:
            // NOTE that we use Json.Net here instead of JsonFx, 
            // due to some issues with JsonFx.
            // RestSearchResults myOtherRes = 
            //  JsonConvert.DeserializeObject<RestSearchResults>(rawresp);
            Console.WriteLine("total # of results: {0}\n\n", nTotResults);
            int nRows = 0;
            foreach (var row in myResults.ResultList)
                if (nRows > 10)
                    "Name:{0}, Distance:{1}, Address1:{2}, City:{3}, State:{4}\n",
                    row.Name, row.Distance, row.Address1, row.City, row.State);
                if (row.Attributes != null)
                    foreach (var attr in row.Attributes)
                        Console.WriteLine("\tid={0}, name={1}, value={2}\n",
                            attr.AttributeId, attr.AttributeName, attr.AttributeValue);
            // or you can use LINQ via JSON.NET:
            Console.WriteLine("\nExtracting location names via JSON:");
            JObject jResults = JObject.Parse(rawresp);
            var jq = from row in jResults["ResultList"].Children()
                     orderby row["Name"]
                     select row["Name"];
            foreach (var rowName in jq)
            if (nRows == 0)
                Console.WriteLine("Empty array returned.\n");
            Console.WriteLine("Press any key...");