Web API响应后如何反序列化JSON对象

时间:2019-07-03 07:13:44

标签: c# arrays json string

对不起,我的英语水平不是最好的...

我正在尝试反序列化从API获得的JSON。 JSON代码如下:

{
  "observations": [
    {
      "stationID": "XXXHEER3",
      "obsTimeUtc": "2019-06-27T20:13:20Z",
      "obsTimeLocal": "2019-06-27 22:13:20",
      "neighborhood": "wh4556",
      "softwareType": "EasyWeatherV1.3.4",
      "country": "DL",
      "solarRadiation": 0.0,
      "lon": 9.10918999,
      "realtimeFrequency": null,
      "epoch": 1561666400,
      "lat": 52.31206894,
      "uv": 0.0,
      "winddir": 359,
      "humidity": 72,
      "qcStatus": 1,
      "metric": {
        "temp": 15,
        "heatIndex": 15,
        "dewpt": 10,
        "windChill": 15,
        "windSpeed": 1,
        "windGust": 2,
        "pressure": 1019.98,
        "precipRate": 0.0,
        "precipTotal": 0.0,
        "elev": 59
      }
    }
  ]
}

最后,我需要不同变量中的值。例如:变量Temperatur =来自temp的值

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using Newtonsoft.Json;
using System.Net.NetworkInformation;

namespace AuslesenDatenAPI
{
class MainClass
{

// I got the class definition from json2csharp.com
public class Wetterstation
{
   public List<Observation> Observations { get; set; }
}

public class Observation
{
   public string StationId { get; set; }
   public DateTimeOffset ObsTimeUtc { get; set; }
   public DateTimeOffset ObsTimeLocal { get; set; }
   public string Neighborhood { get; set; }
   public string SoftwareType { get; set; }
   public string Country { get; set; }
   public double SolarRadiation { get; set; }
   public double Lon { get; set; }
   public object RealtimeFrequency { get; set; }
   public long Epoch { get; set; }
   public double Lat { get; set; }
   public long Uv { get; set; }
   public long Winddir { get; set; }
   public long Humidity { get; set; }
   public long QcStatus { get; set; }
   public Dictionary<string, double> Metric { get; set; }
}

public static void Main()
{

   WebRequest request = WebRequest.Create("blackened");
   WebResponse response = request.GetResponse();               
   Console.WriteLine(((HttpWebResponse)response).StatusDescription);

   Stream newStream = response.GetResponseStream();
   StreamReader sr = new StreamReader(newStream);
   var result = sr.ReadToEnd();

   Console.WriteLine(result);

   var splashInfo = JsonConvert.DeserializeObject<Wetterstation>
   (result);

   Console.WriteLine(splashInfo.Observations[0].Winddir);


}
}
}

在上面的代码中,我得到了结果“ 359”。但是我不知道如何得到“温度”。

如果我尝试

Console.WriteLine(splashInfo.Observations[0].Metric.Temp);

我有一个例外:

“词典”的基本定义“温度”的定义,以及temp-Erweiterungsmethode gefunden werden和ertes参数的vom Typ“ Dictionary” akzeptiert(möglicherpwestive)的用法。 >

0 个答案:

没有答案