簡體   English   中英

試圖分開兩個單獨的字符串

[英]Trying to separate two separate strings

我試圖返回到此 if 語句中的單獨字符串,而不是作為單個字符串。 一個作為緯度,另一個作為經度

static string GeoCoding(string address)
{
    var json = new WebClient().DownloadString(baseUrlGC + address.Replace(" ", "+")
        + plusUrl);//concatenate URL with the input address and downloads the requested resource
    GoogleGeoCodeResponse jsonResult = JsonConvert.DeserializeObject<GoogleGeoCodeResponse>(json); //deserializing the result to GoogleGeoCodeResponse

    string status = jsonResult.status; // get status 

    string geoLocation = String.Empty;

    //check if status is OK
    if (status == "OK") 
    {
        
        for (int i = 0; i < jsonResult.results.Length;i++) //loop throught the result for lat/lng
        {
            geoLocation = jsonResult.results[i].geometry.location.lat + jsonResult.results[i].geometry.location.lng + Environment.NewLine; //append the result addresses to every new line
        }
        return geoLocation; //return result
    }
    else
    {
        return status; //return status / error if not OK
    }
}

假設您的預期結果是 2 個字符串:緯度經度和錯誤時的代碼。 我建議你創建一個新的

class GeoResponse{
List<(string, string)> geocodeList;
string  status;
} 

並將方法的返回類型更改為

static GeoResponse GeoCoding(string address)
        {
            var json = new WebClient().DownloadString(baseUrlGC + address.Replace(" ", "+")
                + plusUrl);//concatenate URL with the input address and downloads the requested resource
            GoogleGeoCodeResponse jsonResult = JsonConvert.DeserializeObject<GoogleGeoCodeResponse>(json); //deserializing the result to GoogleGeoCodeResponse

           

            GeoResponse result = new GeoResponse();
            result.status = jsonResult.status; // get status 

            //check if status is OK
            if (status == "OK")
            {

                for (int i = 0; i < jsonResult.results.Length; i++) //loop throught the result for lat/lng
                {
                    result.geocodeList.Add(jsonResult.results[i].geometry.location.lat, jsonResult.results[i].geometry.location.lng);
                }
            }
            return result;
        }

如果您想在status ok status ok拋出異常,那么您可以這樣做:

static List<Tuple<string, string>> GeoCoding(string address)
{
    var json = new WebClient().DownloadString($"...");
    var jsonResult = JsonConvert.DeserializeObject<GoogleGeoCodeResponse>(json); 

    if (jsonResult.status != "OK")
        throw new Exception($"Request failed with {jsonResult.status}");

    return jsonResult.results
        .Select(result => result.geometry.location)
        .Select(loc => new Tuple<string, string>(loc.lat, loc.lng))
        .ToList();
}

如果你可以使用ValueTuple那么你可以像這樣重寫代碼:

static List<(string Lat, string Long)> GeoCoding(string address)
{
    ...

    return jsonResult.results
        .Select(result => result.geometry.location)
        .Select(loc => (loc.lat, loc.lng))
        .ToList();
}

另請注意, WebClient已被棄用,因此請改用HttpClient


更新 #1

我想 output “N/A” 為 ZERO_RESULTS

static List<(string Lat, string Long)> GeoCoding(string address)
{
    var json = new WebClient().DownloadString($"...");
    var jsonResult = JsonConvert.DeserializeObject<GoogleGeoCodeResponse>(json); 

    if (jsonResult.status == "ZERO_RESULTS")
        return new List<(string, string)> { ("N/A", "N/A") };

    if (jsonResult.status != "OK")
        throw new Exception($"Request failed with {jsonResult.status}");

    return jsonResult.results
        .Select(result => result.geometry.location)
        .Select(loc => (loc.lat, loc.lng))
        .ToList();
}

暫無
暫無

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

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