0

I'm trying to program a console app that will mine data from different weather APIs, write them on screen and after save them to disk. I can display the values from Dark Sky, but I'm not able to get the values from Open Weather.

Below is the code, starting with a class

using System;
using System.Collections.Generic;
using System.Text;

namespace WeatherAPIs.Models
{
    class API_URL
    {
        //This Class takes all the relevant values such as latitude, longitude, Barrie city code, API keys, to crate 
        public static string DarkSkyBaseURL = "https://api.darksky.net/forecast/{Key}/44.4074,-79.6567?units=si";
        public static string OpenWeatherBaseURL = "https://api.openweathermap.org/data/2.5/forecast?id=5894171&units=metric&APPID={Key}";
        public static string WeatherBitBaseURL = "https://api.weatherbit.io/v2.0/current?&lat=44.4074&lon=-79.6567&key={Key}";
    }
}

This is the main code:

using System;
using WeatherAPIs.Models;
using System.Net.Http;
using System.Net;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace WeatherAPIs
{
    class API_Data_Mine
    {
        static void Main(string[] args)
        {
            //Defines The client as WebClient and setup the HTTP Header
            using var client = new WebClient();
            client.Headers.Add("User-Agent", "C# Weather API");
            client.Headers.Add("Accept", "application/json");
            client.Headers.Add("Content-Type", "application/json");

            //Download the JSON result from the DarkSkyBaseURL on API_URL Class
            string darkskyresult = client.DownloadString(API_URL.DarkSkyBaseURL);
            var darkskyjson = JsonConvert.DeserializeObject(darkskyresult);
            //Print int the screen the result of the raw and unbeauty JSON download, wait for the user to press a key to resume
            Console.WriteLine(darkskyresult);
            Console.WriteLine("\npress any key to resume.");
            Console.ReadKey();

            string openWResult = client.DownloadString(API_URL.OpenWeatherBaseURL);
            var openWJson = JsonConvert.DeserializeObject(openWResult);
            //Print int the screen the result of the raw and unbeauty JSON download, wait for the user to press a key to resume
            Console.WriteLine(openWResult);
            Console.WriteLine("\npress any key to resume.");
            Console.ReadKey();

            //Parse and beautify the JSON Output from DarkSky URL
            JObject JDarkSKy = JObject.Parse(darkskyresult);
            JObject JopenW = JObject.Parse(openWResult);

            ////Write the beautified JSON from DarkSKy on screen and wait for the user to press a key to exit
            Console.WriteLine(JDarkSKy);
            Console.WriteLine("\nPress any key to resume.");
            Console.ReadKey();

            Console.WriteLine(JopenW);
            Console.WriteLine("\nPress any key to resume.");
            Console.ReadKey();

            ///Mine the JSON selected JSON data and print it on screen

            var DSTemperature = (string)JDarkSKy.SelectToken("currently.temperature");
            var DSPressure = (string)JDarkSKy.SelectToken("currently.pressure");
            var DSWindSpeed = (string)JDarkSKy.SelectToken("currently.windSpeed");
            var DSRealFeel = (string)JDarkSKy.SelectToken("currently.apparentTemperature");
            var DSUvIndex = (string)JDarkSKy.SelectToken("currently.uvIndex");

            var OWTemperature = (string)JopenW.SelectToken("list.main.temp");
            var OWPressure = (string)JopenW.SelectToken("list.main.pressure");
            var OWWindSpeed = (string)JopenW.SelectToken("list.wind.speed");
            var OWRealFeel = (string)JopenW.SelectToken("list.main.feels_like");

            //UV only works wqith paid keys
            //var OWUvIndex = (string)JDarkSKy.SelectToken("list.main.");

            Console.WriteLine("Temperature on Dark Sky is - " + DSTemperature);
            Console.WriteLine("Pressure on Dark Sky is - " + DSPressure);
            Console.WriteLine("Wind Spped on Dark Sky is - " + DSWindSpeed);
            Console.WriteLine("Real Feel on Dark Sky is - " + DSRealFeel);
            Console.WriteLine("Ultra Violet Index on Dark Sky is - " + DSUvIndex);

            Console.WriteLine("Temperature on Open Weather is - " + OWTemperature);
            Console.WriteLine("Pressure on Open Weather is - " + OWPressure);
            Console.WriteLine("Wind Spped on Open Weather is - " + OWWindSpeed);
            Console.WriteLine("Real Feel on Open Weather is - " + OWRealFeel);

            //Console.WriteLine("\nPress any key to resume.");            
            Console.ReadKey();
        }
    }
}

What I need to do to get the data from Open Weather?

2
  • What is the problem? take a look at the structure of the response and do the same as you did for the others Commented Aug 3, 2020 at 6:32
  • Almost every one of these questions is solved much more simply using app.quicktype.io Commented Aug 3, 2020 at 7:46

1 Answer 1

1

The openweathermap return list as array instead of object. So you should update your select token accordingly.

Try with [0] to fetch data for first object.

var OWTemperature = (string)JopenW.SelectToken("list[0].main.temp");
var OWPressure = (string)JopenW.SelectToken("list[0].main.pressure");
var OWWindSpeed = (string)JopenW.SelectToken("list[0].wind.speed");
var OWRealFeel = (string)JopenW.SelectToken("list[0].main.feels_like");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.