davos
davos

Reputation: 211

Getting properties value for IEnumerator

Partial JSON response – full response follow link (https://www.511virginia.org/data/geojson/icons.rwis.geojson)

    {
   "features": [
      {
         "geometry": {
            "coordinates": [
               "-77.072472",
               "38.799615"
            ],
            "type": "Point"
         },
         "id": "VATLG",
         "properties": {
            "atmos": [
               {
                  "air_temperature": {
                     "units": "Deg F",
                     "value": "48.74"
                  },
                  "dewpoint_temperature": {
                     "units": "Deg F",
                     "value": "28.76"
                  },
                  "observation_time": {
                     "units": "Unixtime",
                     "value": "1577038852"
                  },
                  "relative_humidity": {
                     "units": "Percent",
                     "value": "46"
                  },
                  "visibility": {
                     "units": "Miles",
                     "value": "1.2"
                  },
                  "wind_direction": {
                     "units": "Degrees",
                     "value": "176"
                  },
                  "wind_gust": {
                     "units": "Miles per Hour",
                     "value": 3.13072
                  },
                  "wind_speed": {
                     "units": "Miles per Hour",
                     "value": 0.66758
                  }
               }
            ],
            "description": "I-95 @ Telegraph Rd MM 176.2",
            "name": "Telegraph Rd",
            "surface": [
               {
                  "index": "1",
                  "lane": "1",
                  "observation_time": {
                     "units": "Unixtime",
                     "value": "1577038852"
                  },
                  "surface_condition": {
                     "units": "Text",
                     "value": "Other"
                  },
                  "surface_temperature": {
                     "units": "Deg F",
                     "value": "60.26"
                  },
                  "type": "-9999"
               }
            ]
         },
        “type": "Feature"
      }
   ],
   "processed_time": 1577039280,
   "type": "FeatureCollection"
}

inspecting object

How to I query an iEnumerator object with linq (or iterating through it) to get a value - I'm not searching for a specific temperature, I just want to know what that value is / what the temperature is. In the case / screenshot of the object below "63.86".

I'm also curious what the blue circle next to the value represents - the one with a diagonal white line in it.

The shape of the data is in the image linked.

I can search for the RootObject id with this, var station = featuresList.Where(v => v.id == "VAV01");

But I can not get the current temperature nested in that object....


    public class AirTemperature
    {
        public string units { get; set; }
        public string value { get; set; }
    }

    public class Atmo
    {
        public AirTemperature air_temperature { get; set; }
        public DewpointTemperature dewpoint_temperature { get; set; }
        public ObservationTime observation_time { get; set; }
        public RelativeHumidity relative_humidity { get; set; }
        public Visibility visibility { get; set; }
        public WindDirection wind_direction { get; set; }
        public WindGust wind_gust { get; set; }
        public WindSpeed wind_speed { get; set; }
        public PrecipType precip_type { get; set; }
        public Pressure pressure { get; set; }
    }

    //weather station
    public class Feature
    {
        public Geometry geometry { get; set; }
        public string id { get; set; }
        public Properties properties { get; set; }
        public string type { get; set; }
    }

    //json string that has everything
    public class RootObject
    {
        public List<Feature> features { get; set; }
        public int processed_time { get; set; }
        public string type { get; set; }
    }

    class MainClass
    {
        public static void Main(string[] args)
        { 
            // Instansiate web client
            WebClient client = new WebClient();

            // URL / Endpoint https address
            string UrlEndpointAddress = client.DownloadString("https://www.511virginia.org/data/geojson/icons.rwis.geojson");

            // Call Endpoint & Populate JObject - JSON Weather Object
            JObject JsonWeatherObject = JObject.Parse(UrlEndpointAddress);
            // Search for several specific weather stations
            var VdotMtnWxStations = JsonWeatherObject.SelectTokens("$.features[?(@.id == 'VAV01' || @.id == 'VATLG')]");

            //_______________________________________________

            //create an instance of a list of type Feature
            List<Feature> featuresList = new List<Feature>();
            foreach (JToken x in VdotMtnWxStations)
            {
                //create an instance of feature for each station that is in the featuresList
                Feature feature = new Feature();

                //stuff all the values into a feature object
                feature = x.ToObject<Feature>();

                //add the feature to the feature list
                featuresList.Add(feature);
            }

            var station = featuresList.Where(v => v.id == "VAV01");
        }
    }

Upvotes: 1

Views: 182

Answers (2)

Dongdong
Dongdong

Reputation: 2508

After Paste JSON as classes, you can simply DeserializeObject to your those classes.

public static void Main(string[] args)
{
    WebClient client = new WebClient();
    string jData = client.DownloadString("https://www.511virginia.org/data/geojson/icons.rwis.geojson");

    var data = JsonConvert.DeserializeObject<Rootobject>(jData);

    //find all those features whose air_temperature is less than 63.86
     var val = data.features
        .Where(feature => feature.properties.atmos
            .Any(atmos => atmos.air_temperature != null
                        && Double.Parse(atmos.air_temperature.value) < 63.86))
        .ToList();

    Console.ReadLine();
}

you can see all data is there as expected: enter image description here

Upvotes: 0

ishkin
ishkin

Reputation: 11

Try to deserialise class

Add to class attribute [Serializable] like

[Serializable]
public class RootObject
{
    public List<Feature> features { get; set; }
    public int processed_time { get; set; }
    public string type { get; set; }
}

[Serializable]
    public class AirTemperature
        {
            public string units { get; set; }
            public string value { get; set; }
        }

    [Serializable]
        public class Atmo
        {
            public AirTemperature air_temperature { get; set; }
            public DewpointTemperature dewpoint_temperature { get; set; }
            public ObservationTime observation_time { get; set; }
            public RelativeHumidity relative_humidity { get; set; }
            public Visibility visibility { get; set; }
            public WindDirection wind_direction { get; set; }
            public WindGust wind_gust { get; set; }
            public WindSpeed wind_speed { get; set; }
            public PrecipType precip_type { get; set; }
            public Pressure pressure { get; set; }
        }

        [Serializable]
        public class Feature
        {
            public Geometry geometry { get; set; }
            public string id { get; set; }
            public Properties properties { get; set; }
            public string type { get; set; }
        }

Upvotes: 1

Related Questions