使用Google地理编码结果使用JSON.NET反序列化JSON数据时出现问题 [英] Problem deserializing JSON data with JSON.NET using results from Google geocoding

查看:78
本文介绍了使用Google地理编码结果使用JSON.NET反序列化JSON数据时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为此感到茫然...



这是来自Google的JSON



I'm at a loss for this one ...

Here is the JSON from Google

//{
"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"                 
}





以下是我对应上述的C#类:





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

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:

            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);
         }
      };





字符串jsonGeoCode准确包含有效的JSON字符串(已验证为在各种JSON验证站点)。但是,当我调用DeserializeObject时,结果变量(latongNorthEast和latlongSouthwest)完全不存在...我在Watch窗口中收到以下消息:



当前上下文中不存在名称latlongNorthEast。



我花了太多时间在这上面...请帮助!!!



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!!!

推荐答案

latlongNorthEast 在它声明的使用之外不存在......以及其他所有其他内容!
latlongNorthEast does not exists outside the using in which it declared...and all the others for that matter!


请试试这个..



Please try this ..

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,


这篇关于使用Google地理编码结果使用JSON.NET反序列化JSON数据时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆