首页 > 解决方案 > 如何从列表中获取第二个元素

问题描述

我是 c# 新手,感谢我能得到的所有帮助。程序有效,但我有问题。我使用返回 JSON 的 API。我无法访问第二个“dt”元素。我需要获得第二个“dt”元素。我怎么能在那个代码中做到这一点?

获取数据:

      public static List<Tuple<string, string, string>> euDataDaily(string url)
    {
        var aTuple = new List<Tuple<string, string, string>> { };
        WebClient c = new WebClient();
        var data = c.DownloadString(url);
        JObject o = JObject.Parse(data);

        string conditionCode="";
        string unixDate = "";
        string dayTemp="";

        foreach (var result in o["daily"])
            {
                unixDate = (string)result["dt"];
                    dayTemp = (string)result["temp"]["day"];
                    foreach (var resultas in result["weather"])
                    {
                        conditionCode = (string)resultas["icon"];
                    }  
            }
        List<Tuple<string, string>> icon = convertIcon(conditionCode);

        foreach (var tuple in icon)
        {
            aTuple.Add(Tuple.Create(dayTemp, tuple.Item1, tuple.Item2));
        }
        return aTuple;
    }

JSON 看起来像这样:

        "daily": [
            {
          "dt": 1595268000,
          "sunrise": 1608124431,
          "sunset": 1608160224,
          "temp": {
            "day": 278.14,
            "min": 273.15,
            "max": 279.4,
            "night": 273.15,
            "eve": 275.82,
            "morn": 275.35
          }
          ],
          "clouds": 60,
          "pop": 0.84,
          "uvi": 2.41
          [
            {
          "dt": 1595268001,
          "sunrise": 1608124434,
          "sunset": 1608160224,
          "temp": {
            "day": 278.14,
            "min": 273.15,
            "max": 279.4,
            "night": 273.15,
            "eve": 275.82,
            "morn": 275.35
          }
          ],

....

谢谢你的帮助。

标签: c#jsonapi

解决方案


JSON 无效,但无论如何我都会尝试提供帮助。我将 JSON 格式化为正确的,所以我认为对象模型接近这个:

{
  "daily": [
    {
      "dt": 1595268000,
      "sunrise": 1608124431,
      "sunset": 1608160224,
      "temp": {
        "day": 278.14,
        "min": 273.15,
        "max": 279.4,
        "night": 273.15,
        "eve": 275.82,
        "morn": 275.35
      },
      "weather": {
        "clouds": 60,
        "pop": 0.84,
        "uvi": 2.41,
        "icon": "cloudy.gif"
      }
    },
    {
      "dt": 1595268001,
      "sunrise": 1608124434,
      "sunset": 1608160224,
      "temp": {
        "day": 278.14,
        "min": 273.15,
        "max": 279.4,
        "night": 273.15,
        "eve": 275.82,
        "morn": 275.35
      },
      "weather": {
        "clouds": 60,
        "pop": 0.84,
        "uvi": 2.41,
        "icon": "cloudy.gif"
      }
    }
  ]
}

鉴于该模型,我将创建一个类来表示您需要从模型中获取的字段。这可确保您拥有可以跨代码使用的强类型。

public class WeatherModel
{
    [JsonProperty("daily")]
    public List<daily> Daily { get; set; }

    internal class daily
    {
        [JsonProperty("dt")]
        public long UnixDate { get; set; }

        [JsonProperty("temp")]
        public temp Temp { get; set; }

        [JsonProperty("weather")]
        public weather Weather { get; set; }

        [JsonObject("temp")]
        internal class temp
        {
            [JsonProperty("day")]
            public decimal Day { get; set; }
        }

        [JsonObject("weather")]
        internal class weather
        {
            [JsonProperty("icon")]
            public string Icon { get; set; }
        }
    }
}

一旦有了这些,就很简单了:

public static WeatherModel.daily DataDaily(string url)
{
    var data = new WebClient().DownloadString(url);
    var model = JsonConvert.DeserializeObject<WeatherModel>(data);
    return model.Daily[1]; // Not safe, demo only
}

希望对您有所帮助,欢迎加入社区!


推荐阅读