Read the api in asp.net mvc

I am trying to use weather api in asp.net mvc.

My code is below:

        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;

Failsafe status is ok. However, I do not know how I can see the result (as data or as xml)?

Any help would be appreciated.

Thank.

+4
source share
3 answers

You can create some ViewModels to deserialize it, for a sample:

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

And to deserialize it, you can use RootObjectfor sample:

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

Something like this., Just a different IDEA.

I just tried a different Api call without any Post back to Serverusing Jqueryto get the weather report.

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

GetAsync

var response = await httpClient.GetStringAsync(uri);

This should give you the response data as a string, and if you just need to parse it. You need to use the await keyword to pause the execution of your method until the HTTPClient returns a result.

+1
source

Source: https://habr.com/ru/post/1533718/


All Articles