使用C#读取反序列化的文件

时间:2015-06-11 18:08:29

标签: c# asp.net .net visual-studio

我需要使用Google Maps API和C#重新获得纬度和经度地址。我使用动态获得了这些数据。遵循代码:

public static dynamic GEOCodeAddress(String EnderecoCompleto)
{
    var endereco = String.Format("http://maps.google.com/maps/api/geocode/json?address={0}&sensor=false", EnderecoCompleto.Replace(" ", "+"));
    var result = new System.Net.WebClient().DownloadString(endereco);
    JavaScriptSerializer jss = new JavaScriptSerializer();
    return jss.Deserialize<dynamic>(result);
}

我获得了以下回报:

    {
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Condomínio do Edif Shopping Cent Resende",
               "short_name" : "Condomínio do Edif Shopping Cent Resende",
               "types" : [ "premise" ]
            },
            {
               "long_name" : "369",
               "short_name" : "369",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Avenida Saturnino Braga",
               "short_name" : "Av. Saturnino Braga",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Primeiro",
               "short_name" : "Primeiro",
               "types" : [ "neighborhood", "political" ]
            },
            {
               "long_name" : "Resende",
               "short_name" : "Resende",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Resende",
               "short_name" : "Resende",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Rio de Janeiro",
               "short_name" : "RJ",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Brasil",
               "short_name" : "BR",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "27511300",
               "short_name" : "27511300",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "Condomínio do Edif Shopping Cent Resende - Avenida Saturnino Braga, 369 - Primeiro, Resende - RJ, 27511-300, Brasil",
         "geometry" : {
            "location" : {
               **"lat" : -22.4663045,
               "lng" : -44.4512964**
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : -22.4649555197085,
                  "lng" : -44.4499474197085
               },
               "southwest" : {
                  "lat" : -22.4676534802915,
                  "lng" : -44.45264538029149
               }
            }
         },
         "partial_match" : true,
         "place_id" : "ChIJHTTnrJZ-ngARWHOfZ1DmWGM",
         "types" : [ "premise" ]
      }
   ],
   "status" : "OK"
}

方法调用如下:

dynamic retorno = GEOCodeAddress("Avenida Saturnino Braga, 369 centro, resende - rj");

我需要以粗体显示值。

我的问题是:如何使用C#语言检索此数据并将其转换为String ???

类型

感谢大家的帮助,我为简单的英语道歉。

1 个答案:

答案 0 :(得分:0)

您可以尝试使用Json.NET作为Json解析器(比原生.Net实现更可靠)。只需通过NuGet抓住包裹。

然后尝试使用类似'Dictionary&lt; string,object&gt;'的内容而不是'动态'。 这将使访问对象更容易。 或者只是省略目标类型。然后Json.NET将返回一个JObject类型,也可以通过索引器访问。

最好的方法是创建一个具有该结构的类,并使用Json.NET将JSON反序列化为该类。然后你会有很好的列表/属性来访问。