2013-06-30 21 views
0

我在C#中非常新,我想要做的是構建一個Web服務,當用戶可以放置經緯度並獲取「谷歌類型」。 我想從'address_component'中獲得'type []'。 我該怎麼做?試圖從json反序列化後得到不同類的特定字符串

我的代碼是:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Services; 
using System.Net; 
using System.Web.Script.Serialization; 

using System.Web.Extensions; 
// using System.Web.Script.Serialization.JavaScriptSerializer; 

[WebService(Namespace = "http://idanmoshe.com/", Description = "Idans' web services")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService] 


public class NetroadsWebServices : System.Web.Services.WebService 
{ 
    public NetroadsWebServices() 
    { 
     //Uncomment the following line if using designed components 
     //InitializeComponent(); 
    } 


    [WebMethod (Description = "Get google type by coordinate (road, street etc.)")] 
    public string getGoogleType(float latitude, float longitude) 
    { 
     string completeUrl = "http://maps.googleapis.com/maps/api/geocode/json?latlng=" + latitude + "," + longitude + "&sensor=true"; 
     var jsonString = new WebClient().DownloadString(completeUrl); 
     Console.WriteLine ("Complete URL: "+completeUrl); 

     JavaScriptSerializer jsonParse = new JavaScriptSerializer(); 
     GoogleGeoCodeResponse getTypes = jsonParse.Deserialize<GoogleGeoCodeResponse>(jsonString); 


     return jsonString; 
    } 
} 

public class GoogleGeoCodeResponse 
{ 

    public string status { get; set; } 
    public results[] results { get; set; } 

} 

public class results 
{ 
    public string formatted_address { get; set; } 
    public geometry geometry { get; set; } 
    public string[] types { get; set; } 
    public address_component[] address_components { get; set; } 
} 

public class geometry 
{ 
    public string location_type { get; set; } 
    public location location { get; set; } 
} 

public class location 
{ 
    public string lat { get; set; } 
    public string lng { get; set; } 
} 

public class address_component 
{ 
    public string long_name { get; set; } 
    public string short_name { get; set; } 
    public string[] types { get; set; } 
} 

用於輸出字符串例子:

<?xml version="1.0" encoding="UTF-8"?> 
<string xmlns="http://idanmoshe.com/">{ "results" : [ { "address_components" : [ { "long_name" : "Al Ismaileya - Port Saeed", "short_name" : "Al Ismaileya - Port Saeed", "types" : [ "route" ] }, { "long_name" : "Qesm Awal Al Ganoub", "short_name" : "Qesm Awal Al Ganoub", "types" : [ "administrative_area_level_2", "political" ] }, { "long_name" : "Port Said", "short_name" : "Port Said", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "Egypt", "short_name" : "EG", "types" : [ "country", "political" ] } ], "formatted_address" : "Al Ismaileya - Port Saeed, Qesm Awal Al Ganoub, Port Said, Egypt", "geometry" : { "bounds" : { "northeast" : { "lat" : 31.10762660, "lng" : 32.20366510 }, "southwest" : { "lat" : 31.01789770, "lng" : 32.20137170 } }, "location" : { "lat" : 31.06282650, "lng" : 32.20248240 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 31.10762660, "lng" : 32.20386738029149 }, "southwest" : { "lat" : 31.01789770, "lng" : 32.20116941970849 } } }, "types" : [ "route" ] }, { "address_components" : [ { "long_name" : "Qesm Awal Al Ganoub", "short_name" : "Qesm Awal Al Ganoub", "types" : [ "administrative_area_level_2", "political" ] }, { "long_name" : "Port Said", "short_name" : "Port Said", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "Egypt", "short_name" : "EG", "types" : [ "country", "political" ] } ], "formatted_address" : "Qesm Awal Al Ganoub, Port Said, Egypt", "geometry" : { "bounds" : { "northeast" : { "lat" : 31.34955880, "lng" : 32.28690060 }, "southwest" : { "lat" : 31.01694570, "lng" : 32.05233060 } }, "location" : { "lat" : 31.12950940, "lng" : 32.22000650 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 31.34955880, "lng" : 32.28690060 }, "southwest" : { "lat" : 31.01694570, "lng" : 32.05233060 } } }, "types" : [ "administrative_area_level_2", "political" ] }, { "address_components" : [ { "long_name" : "Port Said", "short_name" : "Port Said", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "Egypt", "short_name" : "EG", "types" : [ "country", "political" ] } ], "formatted_address" : "Port Said, Egypt", "geometry" : { "bounds" : { "northeast" : { "lat" : 31.36418070, "lng" : 32.5629240 }, "southwest" : { "lat" : 30.88772540, "lng" : 32.05233060 } }, "location" : { "lat" : 31.07586060, "lng" : 32.26538870 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 31.36418070, "lng" : 32.5629240 }, "southwest" : { "lat" : 30.88772540, "lng" : 32.05233060 } } }, "types" : [ "administrative_area_level_1", "political" ] }, { "address_components" : [ { "long_name" : "Egypt", "short_name" : "EG", "types" : [ "country", "political" ] } ], "formatted_address" : "Egypt", "geometry" : { "bounds" : { "northeast" : { "lat" : 31.6715350, "lng" : 36.89454460 }, "southwest" : { "lat" : 21.99999990, "lng" : 24.69677480 } }, "location" : { "lat" : 26.8205530, "lng" : 30.8024980 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 31.6715350, "lng" : 36.89454460 }, "southwest" : { "lat" : 21.99999990, "lng" : 24.69677480 } } }, "types" : [ "country", "political" ] } ], "status" : "OK" } </string> 
+0

您可以發佈一個例子字符串? –

+0

我已經添加了一個輸出示例。 –

回答

1
foreach(var result in getTypes.results) 
{ 
    foreach(var address_component in result.address_components) 
    { 
     var types = address_component.types; 
    } 
} 

注:types將字符串的數組。 因此,要提取值,你可以使用像一個明確的指標:types[0]或 迭代通過他們:

foreach(var type in types) 
{ 
    // type is the string 
    Console.WriteLine(type); 
} 
+0

好吧,我怎麼能'var'轉換爲'字符串',因爲我不能'返回'var(隱式類型局部變量必須被初始化)? –

+0

@IdanMoshe查看更新 –

+0

@IdanMoshe你不能返回var,因爲var代表string []。如果你想單個字符串返回,請參閱我的更新 –