Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How can we parse the following json format.

"metadata": 
	{
		"name": "", 
		"latitude": 51.18, 
		"longitude": 3.81, 
		"height": 9, 
		"timezone_abbrevation": "CEST", 
		"utc_timeoffset": 2.00, 
		"modelrun_utc": "4716-04-07 12:00"
	}, 
	"units": 
	{
		"time": "YYYY-MM-DD hh:mm", 
		"pressure": "hPa", 
		"relativehumidity": "percent", 
		"temperature": "C", 
		"precipitation": "mm", 
		"winddirection": "degree", 
		"windspeed": "ms-1"
	}, 
	"history_1h": 
	{
		"time": ["2022-04-01 00:00", "2022-04-01 01:00", "2022-04-01 02:00", "2022-04-01 03:00", "2022-04-01 04:00", "2022-04-01 05:00", "2022-04-01 06:00", "2022-04-01 07:00", "2022-04-01 08:00", "2022-04-01 09:00", "2022-04-01 10:00", "2022-04-01 11:00", "2022-04-01 12:00", "2022-04-01 13:00", "2022-04-01 14:00", "2022-04-01 15:00", "2022-04-01 16:00", "2022-04-01 17:00", "2022-04-01 18:00", "2022-04-01 19:00", "2022-04-01 20:00", "2022-04-01 21:00", "2022-04-01 22:00", "2022-04-01 23:00", "2022-04-02 00:00", "2022-04-02 01:00", "2022-04-02 02:00", "2022-04-02 03:00", "2022-04-02 04:00", "2022-04-02 05:00", "2022-04-02 06:00", "2022-04-02 07:00", "2022-04-02 08:00", "2022-04-02 09:00", "2022-04-02 10:00", "2022-04-02 11:00", "2022-04-02 12:00", "2022-04-02 13:00", "2022-04-02 14:00", "2022-04-02 15:00", "2022-04-02 16:00", "2022-04-02 17:00", "2022-04-02 18:00", "2022-04-02 19:00", "2022-04-02 20:00", "2022-04-02 21:00", "2022-04-02 22:00", "2022-04-02 23:00", "2022-04-03 00:00", "2022-04-03 01:00", "2022-04-03 02:00", "2022-04-03 03:00", "2022-04-03 04:00", "2022-04-03 05:00", "2022-04-03 06:00", "2022-04-03 07:00", "2022-04-03 08:00", "2022-04-03 09:00", "2022-04-03 10:00", "2022-04-03 11:00", "2022-04-03 12:00", "2022-04-03 13:00", "2022-04-03 14:00", "2022-04-03 15:00", "2022-04-03 16:00", "2022-04-03 17:00", "2022-04-03 18:00", "2022-04-03 19:00", "2022-04-03 20:00", "2022-04-03 21:00", "2022-04-03 22:00", "2022-04-03 23:00", "2022-04-04 00:00", "2022-04-04 01:00", "2022-04-04 02:00", "2022-04-04 03:00", "2022-04-04 04:00", "2022-04-04 05:00", "2022-04-04 06:00", "2022-04-04 07:00", "2022-04-04 08:00", "2022-04-04 09:00", "2022-04-04 10:00", "2022-04-04 11:00", "2022-04-04 12:00", "2022-04-04 13:00", "2022-04-04 14:00", "2022-04-04 15:00", "2022-04-04 16:00", "2022-04-04 17:00", "2022-04-04 18:00", "2022-04-04 19:00", "2022-04-04 20:00", "2022-04-04 21:00", "2022-04-04 22:00", "2022-04-04 23:00", "2022-04-05 00:00", "2022-04-05 01:00", "2022-04-05 02:00", "2022-04-05 03:00", "2022-04-05 04:00", "2022-04-05 05:00", "2022-04-05 06:00", "2022-04-05 07:00", "2022-04-05 08:00", "2022-04-05 09:00", "2022-04-05 10:00", "2022-04-05 11:00", "2022-04-05 12:00", "2022-04-05 13:00", "2022-04-05 14:00", "2022-04-05 15:00", "2022-04-05 16:00", "2022-04-05 17:00", "2022-04-05 18:00", "2022-04-05 19:00", "2022-04-05 20:00", "2022-04-05 21:00", "2022-04-05 22:00", "2022-04-05 23:00"], 
		"sealevelpressure": [1000, 1000, 1001, 1002, 1002, 1003, 1004, 1006, 1007, 1008, 1009, 1010, 1011, 1012, 1010, 1011, 1011, 1012, 1012, 1013, 1014, 1015, 1016, 1017, 1017, 1017, 1017, 1017, 1018, 1018, 1018, 1019, 1020, 1021, 1021, 1022, 1022, 1022, 1020, 1020, 1021, 1021, 1021, 1022, 1023, 1023, 1024, 1025, 1025, 1025, 1026, 1026, 1026, 1026, 1026, 1027, 1027, 1027, 1027, 1027, 1027, 1027, 1024, 1024, 1024, 1024, 1023, 1024, 1024, 1024, 1023, 1023, 1023, 1022, 1021, 1021, 1020, 1018, 1017, 1017, 1016, 1015, 1014, 1013, 1012, 1011, 1009, 1009, 1008, 1008, 1007, 1007, 1007, 1007, 1007, 1007, 1007, 1007, 1007, 1008, 1007, 1007, 1007, 1008, 1008, 1009, 1009, 1009, 1010, 1010, 1009, 1008, 1008, 1008, 1007, 1007, 1008, 1007, 1007, 1008], 
		"temperature": [2.13, 2.48, 0.64, 0.70, 0.29, -0.11, -0.52, -0.84, -0.72, 0.44, 0.81, 1.26, 2.34, 3.85, 4.34, 4.62, 5.43, 5.86, 5.56, 5.23, 4.27, 3.35, 2.29, 1.34, 0.91, 0.66, 0.60, 0.08, -0.26, -0.87, -1.57, -2.08, -0.46, 2.68, 4.46, 5.42, 6.04, 6.86, 6.41, 6.66, 6.71, 7.18, 6.44, 6.10, 4.70, 2.80, 1.51, 0.57, -0.72, -1.29, -1.34, -0.98, -0.38, 1.39, 0.58, -0.05, 0.82, 3.25, 4.53, 5.26, 7.02, 7.26, 7.30, 7.50, 8.21, 8.05, 7.58, 6.91, 5.20, 3.32, 2.59, 1.49, 0.24, 0.20, 0.36, 0.72, 1.52, 1.56, 2.34, 2.78, 3.73, 5.44, 5.83, 7.59, 7.67, 7.05, 7.42, 7.92, 8.39, 8.96, 9.28, 10.06, 10.09, 9.80, 9.51, 9.26, 9.35, 9.56, 9.83, 10.21, 10.52, 10.40, 10.16, 9.96, 10.29, 11.35, 11.85, 12.24, 12.82, 13.62, 13.57, 15.03, 14.88, 16.05, 16.24, 15.16, 12.83, 10.83, 10.10, 9.81], 
		"precipitation": [0.00, 0.70, 1.40, 0.90, 0.80, 0.90, 1.00, 0.60, 0.30, 0.10, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.30, 0.60, 0.60, 0.50, 0.40, 1.10, 1.00, 0.10, 0.00, 0.00, 0.00, 0.20, 0.20, 0.20, 0.10, 0.30, 0.60, 0.50, 0.20, 0.10, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.30, 0.10, 0.10, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00], 
		"snowfraction": [1.00, 1.00, 1.00, 1.00, 1.00, 1.00, 1.00, 1.00, 1.00, 1.00, 0.00, 0.00, 0.00, 0.00, 1.00, 1.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00], 
		"winddirection": [42, 47, 48, 49, 49, 49, 47, 48, 52, 50, 51, 49, 46, 46, 46, 46, 42, 40, 40, 39, 35, 33, 31, 31, 29, 26, 26, 26, 26, 29, 33, 36, 36, 44, 48, 45, 39, 35, 32, 27, 27, 28, 22, 17, 14, 19, 20, 23, 25, 23, 17, 12, 360, 353, 352, 326, 283, 270, 323, 333, 346, 354, 355, 347, 348, 345, 340, 331, 320, 299, 268, 248, 239, 234, 234, 233, 236, 236, 234, 235, 239, 239, 240, 238, 245, 246, 247, 248, 251, 254, 254, 253, 250, 249, 252, 251, 259, 261, 264, 270, 277, 280, 279, 278, 283, 286, 291, 295, 296, 294, 274, 270, 272, 274, 268, 261, 254, 250, 248, 251], 
		"windspeed": [4.32, 6.58, 11.54, 15.88, 16.01, 17.15, 16.56, 16.43, 16.16, 15.12, 14.99, 14.10, 14.64, 14.78, 14.78, 13.65, 13.80, 14.26, 14.12, 13.79, 13.12, 12.46, 11.75, 11.08, 10.52, 10.24, 9.88, 9.35, 8.59, 7.65, 6.71, 6.16, 6.30, 7.21, 8.57, 9.19, 9.39, 9.63, 7.55, 7.38, 7.20, 7.25, 7.32, 7.75, 7.20, 5.72, 4.59, 4.03, 3.53, 3.05, 2.72, 2.35, 1.90, 2.42, 2.22, 1.80, 1.84, 2.00, 2.00, 2.46, 3.20, 3.92, 4.72, 5.24, 5.51, 5.70, 5.31, 4.70, 3.41, 2.51, 2.60, 2.92, 3.72, 4.08, 5.08, 6.00, 6.55, 6.85, 7.16, 7.77, 8.44, 9.69, 10.31, 11.10, 11.81, 11.60, 11.38, 10.86, 10.02, 9.59, 9.15, 9.30, 9.14, 9.12, 8.63, 8.35, 7.94, 7.38, 6.94, 7.00, 7.36, 7.32, 6.89, 7.07, 7.38, 7.82, 8.36, 8.28, 8.01, 7.75, 7.42, 7.70, 7.60, 7.62, 7.81, 8.01, 7.47, 7.43, 7.67, 7.40], 
		"relativehumidity": [90, 91, 95, 94, 94, 96, 97, 96, 94, 83, 84, 84, 82, 74, 66, 66, 63, 60, 61, 61, 65, 70, 78, 83, 80, 78, 78, 81, 84, 88, 92, 89, 79, 62, 45, 40, 39, 35, 40, 40, 40, 40, 41, 44, 51, 59, 65, 71, 79, 83, 84, 82, 81, 74, 81, 92, 90, 77, 65, 59, 54, 57, 50, 50, 50, 53, 54, 55, 63, 72, 77, 81, 86, 84, 83, 85, 85, 89, 88, 87, 81, 74, 72, 68, 75, 84, 86, 87, 88, 90, 90, 88, 89, 91, 92, 93, 93, 94, 94, 94, 93, 93, 93, 93, 90, 85, 81, 77, 73, 68, 78, 72, 70, 63, 60, 61, 69, 77, 81, 82]
	}



Need to get the time and respective sealevelpressue.

Can anyone do a favour ?

What I have tried:

C#
var AirObjects = JsonConvert.DeserializeObject<List<Measurements>>(Airresult);


C#
public class Measurements
       {
           public string timestamp { set; get; }
           public decimal value { set; get; }
       }
Posted
Updated 28-Sep-22 3:26am
v2

1 solution

Here are the classes for that JSON data:
C#
    public class Metadata
    {

        [JsonProperty("name")]
        public string Name { getset; }

        [JsonProperty("latitude")]
        public double Latitude { getset; }

        [JsonProperty("longitude")]
        public double Longitude { getset; }

        [JsonProperty("height")]
        public int Height { getset; }

        [JsonProperty("timezone_abbrevation")]
        public string TimezoneAbbrevation { getset; }

        [JsonProperty("utc_timeoffset")]
        public double UtcTimeoffset { getset; }

        [JsonProperty("modelrun_utc")]
        public string ModelrunUtc { getset; }
    }

    public class Units
    {

        [JsonProperty("time")]
        public string Time { getset; }

        [JsonProperty("pressure")]
        public string Pressure { getset; }

        [JsonProperty("relativehumidity")]
        public string Relativehumidity { getset; }

        [JsonProperty("temperature")]
        public string Temperature { getset; }

        [JsonProperty("precipitation")]
        public string Precipitation { getset; }

        [JsonProperty("winddirection")]
        public string Winddirection { getset; }

        [JsonProperty("windspeed")]
        public string Windspeed { getset; }
    }

    public class History1h
    {

        [JsonProperty("time")]
        public IList<string> Time { getset; }

        [JsonProperty("sealevelpressure")]
        public IList<int> Sealevelpressure { getset; }

        [JsonProperty("temperature")]
        public IList<double> Temperature { getset; }

        [JsonProperty("precipitation")]
        public IList<double> Precipitation { getset; }

        [JsonProperty("snowfraction")]
        public IList<double> Snowfraction { getset; }

        [JsonProperty("winddirection")]
        public IList<int> Winddirection { getset; }

        [JsonProperty("windspeed")]
        public IList<double> Windspeed { getset; }

        [JsonProperty("relativehumidity")]
        public IList<int> Relativehumidity { getset; }
    }

    public class Result
    {

        [JsonProperty("metadata")]
        public Metadata Metadata { getset; }

        [JsonProperty("units")]
        public Units Units { getset; }

        [JsonProperty("history_1h")]
        public History1h History1h { getset; }
    }

And to deserialize:
C#
var AirObjects = JsonConvert.DeserializeObject<Result>(Airresult);

I used JSON Utils: Generate C#, VB.Net, SQL TAble and Java from JSON[^] to generate the classes.

For more detailed information on deserializing JSON data, I suggest these 2 articles:
1. Working with Newtonsoft.Json in C# & VB[^]
2. Working with System.Text.Json in C#[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900