【问题标题】:Read deserialized file using C#使用 C# 读取反序列化文件
【发布时间】:2015-06-11 18:08:29
【问题描述】:

我需要使用 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 类型???

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

【问题讨论】:

  • 我投票决定将此问题作为题外话结束,因为它不是英文的。请将其编辑成英文,并以更易读的方式格式化 JSON。 (而且不要在标题中大喊大叫......)

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


【解决方案1】:

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

然后尝试使用类似'Dictionary'而不是'dynamic'。 这将使访问对象更容易。 或者只是省略目标类型。那么Json.NET会返回一个JObject类型,也可以通过索引器访问。

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

【讨论】:

  • 如何使用Json.NET实现?我做了一些测试,但仍然失败。字典我也从未工作过。你会帮我吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-08
  • 1970-01-01
相关资源
最近更新 更多