在json反序列化之后尝试从不同的类中获取特定的字符串

时间:2013-06-30 08:45:27

标签: c# json class

我是C#的新手,我正在尝试做的是建立一个网络服务,当用户可以放置纬度和经度并获得'谷歌类型'。 我想从'address_component'获取'type []'。 我该怎么做?

我的代码是:

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>

1 个答案:

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