在ASP.NET MVC中读取API结果

4

我尝试在asp.net mvc中使用天气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格式)?

任何帮助将不胜感激。

谢谢。


1
API 返回了什么? - Felipe Oriani
1
你知道如何读取 json 结构吗?你可以创建一个 ViewModel 类,并将这个 json 反序列化到这个 ViewModel 中。在这个结构中有很多 ViewModel。 - Felipe Oriani
如果您有任何小例子,我将接受您的答案作为最佳答案。如果您能帮忙,谢谢。 - user3409638
1
请查看以下答案: https://dev59.com/eW445IYBdhLWcg3w9O3R - Daniel Melo
3个回答

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

如果您需要对其进行反序列化,可以使用RootObject,示例如下:

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

回答很好,但是当我设置断点时,var rootobject显示为“{MvcApplication1.Models.rootObject}”。我需要设置列表或类似的东西吗? - user3409638
我已经调整了代码,在这种情况下,请确保引用正确的类型。RootObject类是您需要反序列化的类型。使用rootObject对象来读取值。 - Felipe Oriani
你是最棒的!请给这个人点赞。他是专家。 - user3409638

2

像这样的东西,只是另一个想法。

我刚刚尝试了使用Jquery进行不需要任何服务器回发的不同Api调用来获取天气报告。

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

1
尝试将GetAsync更改为。
var response = await httpClient.GetStringAsync(uri);

这将以字符串形式获取响应数据,如果是json格式,则只需解析即可。您需要使用await关键字,以便在httpClient返回结果之前暂停您的方法的执行。


响应显示了结果,但是它作为字符串在一行中。我该如何逐个获取所有值? - user3409638

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接