[C# JSON] How do I get in

Skythrust

Member
Jul 9, 2019
133
7
Hi there,

I was wondering how I get "temp", "feels_like, "temp_min", "temp_max" out of this JSON string into C#

I would like to use this information in a textbox when I press on a button.

Thanks in advance!

Code:
{
    "coord": {
        "lon": 5.14,
        "lat": 51.69
    },
    "weather": [
        {
            "id": 804,
            "main": "Clouds",
            "description": "overcast clouds",
            "icon": "04d"
        }
    ],
    "base": "stations",
    "main": {
        "temp": 11.09,
        "feels_like": 7.3,
        "temp_min": 11,
        "temp_max": 11.11,
        "pressure": 1005,
        "humidity": 87
    },
    "visibility": 10000,
    "wind": {
        "speed": 5.1,
        "deg": 190
    },
    "clouds": {
        "all": 90
    },
    "dt": 1607956782,
    "sys": {
        "type": 1,
        "id": 1531,
        "country": "UK",
        "sunrise": 1607931548,
        "sunset": 1607959773
    },
    "timezone": 3600,
    "id": 2745123,
    "name": "Bristol",
    "cod": 200
}
 

JayC

Always Learning
Aug 8, 2013
5,493
1,398
Couple ways you could do this. You could do a JSON serializer, where you convert it into a class object, or you could do a LINQ from a parsed JSON.

So if you wanted to use a LINQ statement:

Code:
JObject geographicalJSON = JObject.Parse(json); //json = Your string JSON

double Temperature = (double)geographicalJSON["main"]["temp"];

Or you could make a class structure and have it parse it out for you.

Code:
public class GeographicalInfo
{
    public Main Main {get; set;}
}

public class Main
{
    public double Temp;
    public double Feels_Like;
    public double Temp_Min;
    public double Temp_Max;
    public double Pressure;
    public double Humidity;
}

JavaScriptSerializer js = new JavaScriptSerializer();
GeographicalInfo [] geoInfo =  js.Deserialize<GeographicalInfo[]>(geographicalJSON);
 

Skythrust

Member
Jul 9, 2019
133
7
Couple ways you could do this. You could do a JSON serializer, where you convert it into a class object, or you could do a LINQ from a parsed JSON.

So if you wanted to use a LINQ statement:

Code:
JObject geographicalJSON = JObject.Parse(json); //json = Your string JSON

double Temperature = (double)geographicalJSON["main"]["temp"];

Or you could make a class structure and have it parse it out for you.

Code:
public class GeographicalInfo
{
    public Main Main {get; set;}
}

public class Main
{
    public double Temp;
    public double Feels_Like;
    public double Temp_Min;
    public double Temp_Max;
    public double Pressure;
    public double Humidity;
}

JavaScriptSerializer js = new JavaScriptSerializer();
GeographicalInfo [] geoInfo =  js.Deserialize<GeographicalInfo[]>(geographicalJSON);

Thanks Jay!

I would like to use the one with the class structure. I am getting this message; "System.ArgumentException: 'Invalid JSON Primitive: https.'"

This is my Form code;
 

CosmoPeak

PeakRP.com
May 15, 2016
271
268
Thanks Jay!

I would like to use the one with the class structure. I am getting this message; "System.ArgumentException: 'Invalid JSON Primitive: https.'"

This is my Form code;
You need to perform a HTTP request to get the response from that URL

You can see an example here getting a response as a string using HttpClient:

You would then use your JSON deserialiser, passing in the string response body from your HTTP request as the input, similar to what you have done above.
 
Last edited:

Skythrust

Member
Jul 9, 2019
133
7
You need to perform a HTTP request to get the response from that URL

You can see an example here getting a response as a string using HttpClient:

You would then use your JSON deserialiser, passing in the string response body from your HTTP request as the input, similar to what you have done above to deserialise the JSON into your C# class

Not really sure that I can do something with this information.
Post automatically merged:

By the way... I am using an URL like
 
Last edited:

CosmoPeak

PeakRP.com
May 15, 2016
271
268
You'll need to do something like this:

C#:
var httpClient = new HttpClient();
var response = await httpClient.GetAsync("https://your-weather-url/");
var responseBody = await response.Content.ReadAsStringAsync();
var serializer = new JavaScriptSeralizer();
var geoInfo = serializer.Deseralize<GeographicalInfo[]>(responseBody);
 

Skythrust

Member
Jul 9, 2019
133
7
You'll need to do something like this:

C#:
var httpClient = new HttpClient();
var response = await httpClient.GetAsync("https://your-weather-url/");
var responseBody = await response.Content.ReadAsStringAsync();
var serializer = new JavaScriptSeralizer();
var geoInfo = serializer.Deseralize<GeographicalInfo[]>(responseBody);

But how do I drop this (like the temp (temperature) in textbox?

Sorry I didn't eat that much of C#
 

CosmoPeak

PeakRP.com
May 15, 2016
271
268
But how do I drop this (like the temp (temperature) in textbox?

Sorry I didn't eat that much of C#
I'm not sure what UI framework you're using, so not sure I can help much here. You should be able to access the temp from your object once you've done the above though. It'll be a case of setting the value of the textbox with the temp value.
 

Users who are viewing this thread

Top