How parse REST service JSON response

1. get JSON responses and go to : 

  http://json2csharp.com/

2. write data contracts using C# 

All classes need to have a DataContract attribute, and all public properties that are to be serialized need to have a DataMember attribute, and both a getter and a setter in C#

using System.Runtime.Serialization;

3. serialize the data against data contracts 

The benefit of working with JSON serialization rather than XML serialization is that changes in the schema rarely cause issues for existing applications. For instance, if a new property is added to the REST Services, an application that uses the old data contracts can still process a response without errors; however, the new property will not be available. To get the new property, you must update the data contracts.

4. make HTTP Get request


using System;
using System.Net;
using System.Runtime.Serialization.Json;

namespace RESTServicesJSONParserExample

{
    class Program
    {
        static string BingMapsKey = "insertYourBingMapsKey"; 
        static void Main(string[] args)
        {           
            try
            {
                 string locationsRequest = CreateRequest("New%20York"); 
                Response locationsResponse = MakeRequest(locationsRequest);
                ProcessResponse(locationsResponse);
  }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.Read();
            }

        }

        //Create the request URL
        public static string CreateRequest(string queryString)
        {
            string UrlRequest = "http://dev.virtualearth.net/REST/v1/Locations/" +
                                           queryString +
                                           "?key=" + BingMapsKey;
            return (UrlRequest);
        }

        public static Response MakeRequest(string requestUrl)
        {
            try
            {
                HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    if (response.StatusCode != HttpStatusCode.OK)
                        throw new Exception(String.Format(
                        "Server error (HTTP {0}: {1}).",
                        response.StatusCode,
                        response.StatusDescription));
                    DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(Response));
                    object objResponse = jsonSerializer.ReadObject(response.GetResponseStream());
                    Response jsonResponse
                    = objResponse as Response;
                    return jsonResponse;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return null;
            }

        }

        static public void ProcessResponse(Response locationsResponse)
        {

            int locNum = locationsResponse.ResourceSets[0].Resources.Length;

            //Get formatted addresses: Option 1
            //Get all locations in the response and then extract the formatted address for each location
            Console.WriteLine("Show all formatted addresses");
            for (int i = 0; i < locNum; i++)
            {
                Location location = (Location)locationsResponse.ResourceSets[0].Resources[i];
                Console.WriteLine(location.Address.FormattedAddress);
            }
            Console.WriteLine();
            
            //Get the Geocode Points for each Location
            for (int i = 0; i < locNum; i++)
            {
               Location location = (Location)locationsResponse.ResourceSets[0].Resources[i];
               Console.WriteLine("Geocode Points for "+location.Address.FormattedAddress);               
               int geocodePointNum = location.GeocodePoints.Length;
               for (int j = 0; j < geocodePointNum; j++)
               {
                   Console.WriteLine("    Point: "+location.GeocodePoints[j].Coordinates[0].ToString()+","+
                                                location.GeocodePoints[j].Coordinates[1].ToString());
                   double test = location.GeocodePoints[j].Coordinates[1];
                   Console.Write("    Usage: ");              
                   for (int k = 0; k < location.GeocodePoints[j].UsageTypes.Length; k++)
                   {                                                     
                      Console.Write(location.GeocodePoints[j].UsageTypes[k].ToString()+" ");
                   }
                   Console.WriteLine("\n\n");
               }
            }
            Console.WriteLine();


            //Get all locations that have a MatchCode=Good and Confidence=High
            Console.WriteLine("Locations that have a Confidence=High");
            for (int i = 0; i < locNum; i++)
            {
                Location location = (Location)locationsResponse.ResourceSets[0].Resources[i];
                if (location.Confidence == "High")
                    Console.WriteLine(location.Address.FormattedAddress);
            }
                        Console.WriteLine();

            Console.WriteLine("Press any key to exit");
            Console.ReadKey();


        }
    }
}

 

posted @ 2013-10-11 06:05  MinieGoGo  阅读(310)  评论(0编辑  收藏  举报