【问题标题】:Getting properties value for IEnumerator获取 IEnumerator 的属性值
【发布时间】:2020-04-17 02:22:32
【问题描述】:

部分 JSON 响应 – 完整响应跟随链接 (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"
}

如何使用 linq(或遍历它)查询 iEnumerator 对象以获取值 - 我不是在搜索特定温度,我只想知道该值是什么/温度是多少。在“63.86”以下对象的案例/截图中。

我也很好奇值旁边的蓝色圆圈代表什么 - 里面有对角白线的那个。

数据的形状在链接的图像中。

我可以用这个搜索 RootObject id,var station = featuresList.Where(v => v.id == "VAV01");

但我无法获得嵌套在该对象中的当前温度....


    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");
        }
    }

【问题讨论】:

  • 准确提问。不要把整个代码放在这里。只需使用 Response Json 编辑您的问题
  • Sushant - 我该如何做 string temperature station = featuresList.---- 或遍历它以获得上图中显示的当前温度?
  • 为什么不将下载的字符串反序列化为强类型,而不是在json api上浪费时间?

标签: c# .net-core ienumerator


【解决方案1】:

Paste JSON as classes 之后,您可以简单地DeserializeObject 加入您的这些课程。

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();
}

您可以看到所有数据都如预期的那样:

【讨论】:

  • 是的,我可以看到所有数据,我正在尝试检索特定值 - 例如一个站点的实际空气温度,我已经挂断了。[屏幕帽] [1]:i.stack.imgur.com/Q1e5o.jpg
  • 你期待什么结果? List 还是 List?或列出?@davos
  • 对不起,我不确定我是否从根本上关注你,我预计会产生这样的效果: string airTemp=val.properties.atmos.air_temperature.value 其中字符串等于当前温度,即68.3
  • 答案已更新。但你的要求现在已经不在你的考虑范围内了。轮到 Linq 技能了。@davos,如你所见,88 个中有 82 个被选中。我不完全知道你想要哪一个,但现在足以回答你当前的帖子问题。
  • 我刚刚看到了我错过的这段代码 我在午饭后尝试它.. var val = data.features .Where(x => x.properties.atmos .Any(x => x. air_temperature != null && Double.Parse(x.air_temperature.value)
【解决方案2】:

尝试反序列化类

添加到类属性[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; }
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-10
    • 2016-07-23
    • 2023-03-30
    • 1970-01-01
    • 2013-07-21
    • 2017-02-14
    相关资源
    最近更新 更多