2014-03-27 52 views
4

我嘗試在asp.net mvc中使用天氣api。閱讀api導致asp.net mvc

我下面的代碼:

 var url = "http://api.worldweatheronline.com/free/v1/weather.ashx?q=London&format=json&num_of_days=5&key=6b87pfhmjb7ydj6w596fujpu"; 
     var client = new HttpClient(); 

     client.DefaultRequestHeaders.Accept.Add(
      new MediaTypeWithQualityHeaderValue("application/json")); 

     var response = client.GetAsync(url).Result; 

效應初探狀態爲「OK」。但是我不知道我怎麼能看到結果(數據或XML)?

任何幫助將不勝感激。

謝謝。

+1

APi正在返回什麼? –

+0

它返回這個http://api.worldweatheronline.com/free/v1/weather.ashx?q=London&format=json&num_of_days=5&key=6b87pfhmjb7ydj6w596fujpu – user3409638

+1

你知道如何閱讀'json'結構嗎?你可以創建一個ViewModel類,並將這個'json'反序列化到這個ViewModel中。這個結構上有很多VieWModels。 –

回答

4

你可以創建一些的ViewModels到deserializa它,樣品:

public class WeatherDesc 
{ 
    public string value { get; set; } 
} 

public class WeatherIconUrl 
{ 
    public string value { get; set; } 
} 

public class CurrentCondition 
{ 
    public string cloudcover { get; set; } 
    public string humidity { get; set; } 
    public string observation_time { get; set; } 
    public string precipMM { get; set; } 
    public string pressure { get; set; } 
    public string temp_C { get; set; } 
    public string temp_F { get; set; } 
    public string visibility { get; set; } 
    public string weatherCode { get; set; } 
    public List<WeatherDesc> weatherDesc { get; set; } 
    public List<WeatherIconUrl> weatherIconUrl { get; set; } 
    public string winddir16Point { get; set; } 
    public string winddirDegree { get; set; } 
    public string windspeedKmph { get; set; } 
    public string windspeedMiles { get; set; } 
} 

public class Request 
{ 
    public string query { get; set; } 
    public string type { get; set; } 
} 

public class WeatherDesc2 
{ 
    public string value { get; set; } 
} 

public class WeatherIconUrl2 
{ 
    public string value { get; set; } 
} 

public class Weather 
{ 
    public string date { get; set; } 
    public string precipMM { get; set; } 
    public string tempMaxC { get; set; } 
    public string tempMaxF { get; set; } 
    public string tempMinC { get; set; } 
    public string tempMinF { get; set; } 
    public string weatherCode { get; set; } 
    public List<WeatherDesc2> weatherDesc { get; set; } 
    public List<WeatherIconUrl2> weatherIconUrl { get; set; } 
    public string winddir16Point { get; set; } 
    public string winddirDegree { get; set; } 
    public string winddirection { get; set; } 
    public string windspeedKmph { get; set; } 
    public string windspeedMiles { get; set; } 
} 

public class Data 
{ 
    public List<CurrentCondition> current_condition { get; set; } 
    public List<Request> request { get; set; } 
    public List<Weather> weather { get; set; } 
} 

public class RootObject 
{ 
    public Data data { get; set; } 
} 

而對於deserializa它,你可以使用RootObject,樣品:

var response = client.GetStringAsync(url); 
var rootObject = JsonConvert.DeserializeObject<RootObject >(response.Result); 
+0

很好的答案,但當我放置斷點時,var rootobject說「{MvcApplication1.Models.rootObject}」。我是否需要設置列表或類似的東西? – user3409638

+0

我調整了代碼,在這種情況下,請確保您引用的是正確的類型。 'RootObject'類是您需要反序列化的類型。使用'rootObject'對象來讀取值。 –

+0

你是最棒的!請給這個人+點。他是專家。 – user3409638

1

嘗試改變GetAsync到

var response = await httpClient.GetStringAsync(uri); 

這應該讓你的響應數據作爲一個字符串,如果它是JSON你只需要解析它。您需要使用await關鍵字,以便暫停執行您的方法,直到httpClient返回結果。

+0

reponse顯示我的結果,但它在1行作爲string.How我可以得到所有一個一個的值? – user3409638

2

這樣的事情,只是另一個IDEA。

我剛剛嘗試了不同的Api call without any Post back to Server使用Jquery獲取天氣報告。

<script> 
    $(document).ready(function() { 
     $('#btnGetWeather').click(function() { 
      $.post('http://api.openweathermap.org/data/2.5/weather?q=' + $('#txtCityName').val() + "," + $('#txtCountryCode').val(), function (data) { 
       $('#lblTempMax').text(data.main.temp_max); 
       $('#lblTempMin').text(data.main.temp_min); 
       $('#lblSunRise').text(msToTime(data.sys.sunrise)); 
       $('#lblSunSet').text(msToTime(data.sys.sunset)); 
      }); 

      return false; 
     }); 

     function msToTime(s) { 
      var milli = s * 1000; 
      return new Date(milli); 
     } 
    }); 
</script>