在asp.net mvc中读取api结果

时间:2014-03-27 11:48:37

标签: c# asp.net-mvc api weather

我尝试在asp.net mvc中使用weather api。

我的代码如下:

        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)?

任何帮助将不胜感激。

感谢。

3 个答案:

答案 0 :(得分:4)

您可以创建一些ViewModel来反序列化它,样本:

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);

答案 1 :(得分: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>

答案 2 :(得分:1)

尝试将GetAsync更改为

var response = await httpClient.GetStringAsync(uri);

那应该将响应数据作为字符串提供给你,如果是json,你只需要解析它。您需要使用await关键字,以便在httpClient返回结果之前暂停执行方法。