Click here to Skip to main content
15,072,627 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm at a loss for this one ...

Here is the JSON from Google

C#
//{
"results":[                           
          {
          "address_components": [                                   
                            {
                               "long_name":"1600",
                               "short_name":"1600",
                               "types":[ "street_number" ]
                            },                                      
                            {                                               
                               "long_name":"Amphitheatre Parkway",
                               "short_name":"Amphitheatre Pky",
                               "types":[ "route" ] 
                            },                                     
                            {                                  
                               "long_name":"Mountain View",
                               "short_name":"Mountain View",
                               "types":[ "locally", "political" ]
                            },                                    
                            {                                    
                               "long_name":"California",
                               "short_name":"CA",
                               "types":[ "administrative_area_level_1", "political" ]
                            },                                    
                            {                                        
                               "long_name":"United States",
                               "short_name":"US",
                               "types":[ "country", "political" ]
                            },                                    
                            {                                
                               "long_name":"94043",
                               "short_name":"94043",
                               "types":[ "postal_code" ]
                            }                        
                            ],
      "formatted_address":"1600 Amphitheatre Parkway, Mountain View, CA 94043, USA",
      "geometry": {                    
                   "location": {
                                "lat": 37.4219985, 
                                "lng": -122.0839544
                               },
                   "location_type": "ROOFTOP",
                   "viewport": {
                                "northeast" :{  
                                              "lat": 37.4233474802915,
                                              "lng": -122.0826054197085
                                             }, 
                                "southwest" :{     
                                              "lat": 37.4206495197085,
                                              "lng": -122.0853033802915
                                             }
                               }
                                    
                  },   
      "types": ["street_address"]
   }          
   ],             
"status" : "OK"                 
}


And here are my C# classes that correspond to the above:

C#
public class AddressComponent
{
    public string long_name { get; set; }
    public string short_name { get; set; }
    public List<string> types { get; set; }
}

public class Location
{
    public double lat { get; set; }
    public double lng { get; set; }
}

public class Northeast
{
    public double lat { get; set; }
    public double lng { get; set; }
}

public class Southwest
{
    public double lat { get; set; }
    public double lng { get; set; }
}

public class Viewport
{
    public Northeast northeast { get; set; }
    public Southwest southwest { get; set; }
}

public class Geometry
{
    public Location location { get; set; }
    public string location_type { get; set; }
    public Viewport viewport { get; set; }
}

public class Result
{
    public List<AddressComponent> address_components { get; set; }
    public string formatted_address { get; set; }
    public Geometry geometry { get; set; }
    public List<string> types { get; set; }
}

public class RootObject
{
    public List<Result> results { get; set; }
    public string status { get; set; }
}


And finally, here is my code:

C#
            var request = WebRequest.Create("http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true");
request.ContentType = "application/json; charset=utf-8";
            
string jsonGeoCode;
using (var response = (HttpWebResponse)request.GetResponse()) 
      {
         using (var sr = new StreamReader(response.GetResponseStream()))
         {
         jsonGeoCode = sr.ReadToEnd();
         var latlongNorthEast = JsonConvert.DeserializeObject<Northeast>( jsonGeoCode);
         var latlongSouthwest = JsonConvert.DeserializeObject<Southwest>( jsonGeoCode);
         var rootObject       = JsonConvert.DeserializeObject<RootObject>(jsonGeoCode);
         }
      };


The string jsonGeoCode accurately contains a valid JSON string (verified this at various JSON validation sites). However, when I make the call to DeserializeObject the resulting variables (latongNorthEast and latlongSouthwest) completely cease to exist ... I get the following message in the Watch window:

The name "latlongNorthEast" does not exist in the current context.

I've spent too many hours on this ... please help!!!
Posted

latlongNorthEast does not exists outside the using in which it declared...and all the others for that matter!
   
Please try this ..

C#
var request = WebRequest.Create("http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true");
request.ContentType = "application/json; charset=utf-8";

string jsonGeoCode;
using (var response = (HttpWebResponse)request.GetResponse())
      {
         using (var sr = new StreamReader(response.GetResponseStream()))
         {
         jsonGeoCode = sr.ReadToEnd();
         var rootObject = JsonConvert.DeserializeObject<RootObject>( jsonGeoCode);
        
         //Iterate over the rootObject -> results and then access geometry -> viewport property.
         // The viewport has the Northeast and Southwest information.
         }
      };



Thanks,
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900