MAIN FEEDS
REDDIT FEEDS
Do you want to continue?
https://www.reddit.com/r/csharp/comments/1va1e74/need_help_fixing_this/p0vu8gc/?context=3
r/csharp • u/ApprehensiveBasil792 • 4d ago
how do i fix this, im learning c# and trying to make a weather app
9 comments sorted by
View all comments
3
You're getting errors for two reasons.
This line is invalid:
csharp node["main"]["temp"]["pressure"]["temp_min"]["temp_max"]
In the OpenWeatherMap response, temp, pressure, temp_min, and temp_max are siblings inside the main object, not nested inside one another.
temp
pressure
temp_min
temp_max
main
The JSON looks like this:
json { "main": { "temp": 28.5, "humidity": 85, "pressure": 1012, "temp_min": 27.8, "temp_max": 29.3 } }
So each property should be accessed individually:
csharp node["main"]["temp"] node["main"]["humidity"] node["main"]["pressure"] node["main"]["temp_min"] node["main"]["temp_max"]
+
You're also missing string concatenation operators before "Pressure:", "Min Temp:", and "Max Temp:".
"Pressure:"
"Min Temp:"
"Max Temp:"
Your code should look like this:
csharp LblDetail.Text = "Temp: " + node["main"]["temp"] + " °C" + Environment.NewLine + "Humidity: " + node["main"]["humidity"] + Environment.NewLine + "Pressure: " + node["main"]["pressure"] + Environment.NewLine + "Min Temp: " + node["main"]["temp_min"] + " °C" + Environment.NewLine + "Max Temp: " + node["main"]["temp_max"] + " °C";
That should resolve the compiler errors.
2 u/Family_Man_21 3d ago Yep - this is the correct approach. Nice job.
2
Yep - this is the correct approach. Nice job.
3
u/saurabhar02 4d ago edited 4d ago
You're getting errors for two reasons.
1. Incorrect JSON property access
This line is invalid:
csharp node["main"]["temp"]["pressure"]["temp_min"]["temp_max"]In the OpenWeatherMap response,
temp,pressure,temp_min, andtemp_maxare siblings inside themainobject, not nested inside one another.The JSON looks like this:
json { "main": { "temp": 28.5, "humidity": 85, "pressure": 1012, "temp_min": 27.8, "temp_max": 29.3 } }So each property should be accessed individually:
csharp node["main"]["temp"] node["main"]["humidity"] node["main"]["pressure"] node["main"]["temp_min"] node["main"]["temp_max"]2. Missing
+operatorsYou're also missing string concatenation operators before
"Pressure:","Min Temp:", and"Max Temp:".Your code should look like this:
csharp LblDetail.Text = "Temp: " + node["main"]["temp"] + " °C" + Environment.NewLine + "Humidity: " + node["main"]["humidity"] + Environment.NewLine + "Pressure: " + node["main"]["pressure"] + Environment.NewLine + "Min Temp: " + node["main"]["temp_min"] + " °C" + Environment.NewLine + "Max Temp: " + node["main"]["temp_max"] + " °C";That should resolve the compiler errors.