簡體   English   中英

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

[英]Read deserialized file using C#

我需要使用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.NET用作Json解析器(比本機.Net實現更可靠)。 只需通過NuGet抓取包裝即可。

然后嘗試使用“ Dictionary <string,object>”之類的東西代替“ dynamic”。 這將使訪問對象更加容易。 或者只是忽略目標類型。 然后Json.NET將返回一個JObject類型,該類型也可以通過索引器進行訪問。

最好的方法是使用該結構創建一個類,然后使用Json.NET將您的JSON反序列化為該結構。 這樣,您將可以訪問不錯的列表/屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM