【问题标题】:How do I get the "temperature" if I have the "regio" as input如果我有“regio”作为输入,我如何获得“温度”
【发布时间】:2018-12-23 09:07:52
【问题描述】:
{
  "buienradar": {
    "copyright": "(C)opyright Buienradar / RTL. Alle rechten voorbehouden",
    "terms": "Deze feed mag vrij worden gebruikt onder voorwaarde van bronvermelding buienradar.nl inclusief een hyperlink naar https://www.buienradar.nl. Aan de feed kunnen door gebruikers of andere personen geen rechten worden ontleend."
  },
  "actual": {
    "stationmeasurements": [
       {
        "stationid": 6330,
        "stationname": "Meetstation Hoek van Holland",
        "lat": 51.98,
        "lon": 4.1,
        "regio": "Hoek van Holland",
        "timestamp": "2018-12-23T09:40:00",
        "dayhistory": {
          "timestamp": "2018-12-23T01:00:00",
          "temperatureMin": 7.3,
          "temperatureMax": 8.8,
          "groundtemperatureMin": 6.9,
          "sunshineHours": 0.0,
          "windgustsMax": 9.71,
          "windspeedMax": 7.81,
          "windspeedBftMax": 4,
          "windDirectionDegreesMax": 223
        }
      },
      {
        "stationid": 6279,
        "stationname": "Meetstation Hoogeveen",
        "lat": 52.73,
        "lon": 6.52,
        "regio": "Hoogeveen",
        "timestamp": "2018-12-23T09:40:00",
        "dayhistory": {
          "timestamp": "2018-12-23T01:00:00",
          "temperatureMin": 6.0,
          "temperatureMax": 7.5,
          "groundtemperatureMin": 5.7,
          "sunshineHours": 0.0,
          "windgustsMax": 7.21,
          "windspeedMax": 5.05,
          "windspeedBftMax": 3,
          "windDirectionDegreesMax": 255
        }
      },

所以我想请求温度,但我似乎无法找出如何从具有特定“区域”的结构中获取所有 json 数据。我是使用 foreach 循环还是一些 LINQ 表达式?这是针对不和谐机器人的。 API 端点是https://api.buienradar.nl/data/public/2.0/jsonfeed

【问题讨论】:

    标签: c# json linq


    【解决方案1】:

    您可以反序列化 JSON,然后查询反序列化的对象以获取所需信息。您可以从定义类结构开始。

    public class Buienradar
    {
        public string copyright { get; set; }
        public string terms { get; set; }
    }
    
    public class Dayhistory
    {
        public DateTime timestamp { get; set; }
        public double temperatureMin { get; set; }
        public double temperatureMax { get; set; }
        public double groundtemperatureMin { get; set; }
        public double sunshineHours { get; set; }
        public double windgustsMax { get; set; }
        public double windspeedMax { get; set; }
        public int windspeedBftMax { get; set; }
        public int windDirectionDegreesMax { get; set; }
    }
    
    public class Stationmeasurement
    {
        public int stationid { get; set; }
        public string stationname { get; set; }
        public double lat { get; set; }
        public double lon { get; set; }
        public string regio { get; set; }
        public DateTime timestamp { get; set; }
        public Dayhistory dayhistory { get; set; }
    }
    
    public class Actual
    {
        public List<Stationmeasurement> stationmeasurements { get; set; }
    }
    
    public class RootObject
    {
        public Buienradar buienradar { get; set; }
        public Actual actual { get; set; }
    }
    

    您可以使用 Newtonsoft.Json 反序列化 Json 字符串。

    var deserializedObject = JsonConvert.DeserializeObject<RootObject>(str);
    

    最后,您可以使用 LINQ 查询所需区域的温度。

    var result = deserializedObject.actual
                                   .stationmeasurements
                                   .Where(x=>x.regio.Equals("Hoogeveen"))
                                   .Select(x=>new 
                                   { 
                                     MinTemperature = x.dayhistory.temperatureMin, 
                                     MaxTemperature = x.dayhistory.temperatureMax
                                    } 
                                  );
    

    输出

    MinTemperature 6
    
    MaxTemperature 7.5
    

    【讨论】:

      【解决方案2】:

      不使用反序列化:

      var regio = "Hoogeveen";
      
      using (var client = new HttpClient())
      {
          var response = await client.GetAsync("https://api.buienradar.nl/data/public/2.0/jsonfeed");
          var result = await response.Content.ReadAsStringAsync();
      
          var json = JObject.Parse(result);
      
          var stationmeasurement = json["actual"]["stationmeasurements"]
              .FirstOrDefault(token => token["regio"].Value<string>() == regio);
      
          var temperature = stationmeasurement["temperature"].Value<double>();
      
          Console.WriteLine(temperature);
      }
      

      使用了 Json.net 库。

      【讨论】:

      • 谢谢我自己找到了方法。无论如何感谢您的时间
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-04
      • 1970-01-01
      • 2014-03-31
      相关资源
      最近更新 更多