Json child 在 C# 中访问雅虎天气 json

Json child access yahoo weather json in C#

所以我试图从 Yahoo 的天气中获取天气 Json,但问题是我一直收到此错误

{"Cannot access child value on Newtonsoft.Json.Linq.JValue."}

现在我不知道为什么会这样。我已经检查了几次育儿,拼写等等。

    public String GetWeather() {
        StringBuilder theWebAddress = new StringBuilder();
        theWebAddress.Append("https://query.yahooapis.com/v1/public/yql?");
        theWebAddress.Append("q=" + System.Web.HttpUtility.UrlEncode("select * from weather.forecast where woeid in (select woeid from geo.places(1) where text='"+ city + ", "+ state + "') and u='" + units +"'"));
        theWebAddress.Append("&format=json");
        theWebAddress.Append("&diagnostics=false");

        string results = "";

        using (WebClient wClient = new WebClient())
        {
            results = wClient.DownloadString(theWebAddress.ToString());
        }

        JObject dataObject = JObject.Parse(results);
        JArray jsonArray = (JArray)dataObject["query"]["results"]["channel"];        //This is the line that is generating the error.


        foreach (var woeid in jsonArray)
        {
            //stocheaza informatiile in variabile
            condition = woeid["item"]["condition"]["text"].ToString();
            //System.Diagnostics.Debug.WriteLine(condition);
            return condition;
        }
        return null;
    }

link 到 API 是 here。据我所知,获取查询或结果的 child 存在问题。有任何想法吗?提前致谢。

我改代码解决了。我没有使用该代码,而是将其更改为

public string GetWeather(string info)
    {
        string results = "";

        using (WebClient wc = new WebClient())
        {
            results = wc.DownloadString("https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%27galati%2C%20ro%27)%20and%20u%3D%22c%22&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys");
        }

        dynamic jo = JObject.Parse(results);

        if (info == "cond")
        {
            var items = jo.query.results.channel.item.condition;
            condition = items.text;

            return condition;
        }
    }

现在它按预期工作了。