C
C#13mo ago
GIGA BRAIN

❔ Accessing json results for a weather api console app

https://paste.mod.gg/jtzzrouisnhf/0 I'm trying to access the temperatures, but when I try to get back a list according to how i have it in the WeatherInfo.cs class, it doesn't work. Any suggestions?
BlazeBin - jtzzrouisnhf
A tool for sharing your source code with the world!
10 Replies
daysleeper
daysleeper13mo ago
mapping is wrong, you send 3 get requests to 1 url and map it into different objects. solution would be to map it into one object {"latitude":33.98601,"longitude":-117.748215,"generationtime_ms":0.3509521484375,"utc_offset_seconds":-25200,"timezone":"America/Los_Angeles","timezone_abbreviation":"PDT","elevation":295.0,"daily_units":{"time":"iso8601","temperature_2m_max":"°F"},"daily":{"time":["2023-06-17","2023-06-18","2023-06-19","2023-06-20","2023-06-21","2023-06-22","2023-06-23"],"temperature_2m_max":[86.3,79.6,76.9,80.0,81.4,77.6,78.2]}}

class DailyUnits
{
string time;
string temperature_2m_max;
}

class Daily
{
List<string> time; // or List<DateTime>
List<double> temperature_2m_max;
}

class Weather
{
double latitude;
double longitude;
double generationtime_ms;
int utc_offset_seconds;
string timezone;
string timezone_abbreviation;
double elevation;
DailyUnits dayly_units;
Daily daily;
}

class DailyUnits
{
string time;
string temperature_2m_max;
}

class Daily
{
List<string> time; // or List<DateTime>
List<double> temperature_2m_max;
}

class Weather
{
double latitude;
double longitude;
double generationtime_ms;
int utc_offset_seconds;
string timezone;
string timezone_abbreviation;
double elevation;
DailyUnits dayly_units;
Daily daily;
}
should be something like that
GIGA BRAIN
GIGA BRAIN13mo ago
so to map into one object, would it be a single get request into an object of the WeatherInfo class? and i decided not to include some of those properties because i was told i could ignore them if i didn't want them in this case i only want what i ahve in my current class, but when i call the api it gives me stuff i dont want
daysleeper
daysleeper13mo ago
you can exclude some properties of you dont want them
GIGA BRAIN
GIGA BRAIN13mo ago
oh i think i see it now, so i want to call dailyunits and daily in the weather class and that would be just one object that i could call everything from i'm confused on how i would access each daily and dailyunits entry though
var rootResults = await client.GetFromJsonAsync<Root>("https://api.open-meteo.com/v1/forecast?latitude=33.99&longitude=-117.76&daily=temperature_2m_max&temperature_unit=fahrenheit&timezone=America%2FLos_Angeles");

Console.WriteLine(rootResults.latitude);
Console.WriteLine(rootResults.longitude);
Console.WriteLine(rootResults.timezone);
Console.WriteLine(rootResults.daily);
var rootResults = await client.GetFromJsonAsync<Root>("https://api.open-meteo.com/v1/forecast?latitude=33.99&longitude=-117.76&daily=temperature_2m_max&temperature_unit=fahrenheit&timezone=America%2FLos_Angeles");

Console.WriteLine(rootResults.latitude);
Console.WriteLine(rootResults.longitude);
Console.WriteLine(rootResults.timezone);
Console.WriteLine(rootResults.daily);
GIGA BRAIN
GIGA BRAIN13mo ago
from this my output is
daysleeper
daysleeper13mo ago
rootResults.daily.time[0]
GIGA BRAIN
GIGA BRAIN13mo ago
wow that was exactly what i needed thank you
daysleeper
daysleeper13mo ago
no problem $close
MODiX
MODiX13mo ago
Use the /close command to mark a forum thread as answered
Accord
Accord13mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.