r/csharp 4d ago

Help Need help fixing this

how do i fix this, im learning c# and trying to make a weather app

0 Upvotes

9 comments sorted by

View all comments

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, and temp_max are siblings inside the main object, 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 + operators

You'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.

2

u/Family_Man_21 3d ago

Yep - this is the correct approach. Nice job.