如果输入“ regio”,如何获得“温度”

时间:2018-12-23 09:07:52

标签: c# json linq

{
  "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

2 个答案:

答案 0 :(得分:2)

您可以反序列化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

答案 1 :(得分:0)

不使用反序列化:

let sixVideoViewController = AVPlayerViewController()    


// SIX VIDEO

@IBAction func sixVideoPlayButton(_ sender: Any) {

    let sixVideoURL = Bundle.main.url(forResource: "Six", withExtension: 
"mp4")!
    let sixVideoPlayer = AVPlayer(url: sixVideoURL as URL)

    sixVideoViewController.player = sixVideoPlayer

    NotificationCenter.default.addObserver(self, selector: 
#selector(sixVideoPlayerDidFinishPlaying), name: 
NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: 
sixVideoViewController.player?.currentItem)

    self.present(sixVideoViewController, animated: true) {
        self.sixVideoViewController.player!.play()
    }
}


@objc func sixVideoPlayerDidFinishPlaying(note: NSNotification) {
    self.sixVideoViewController.dismiss(animated: true)

}

使用了Json.net库。