【发布时间】: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