C# Json子访问yahoo weather Json在C中#

C# Json子访问yahoo weather Json在C中#,c#,json,linq,yahoo,C#,Json,Linq,Yahoo,所以我试图从Yahoo的weather Json中获取天气信息,但问题是我一直得到这个错误 {“无法访问Newtonsoft.Json.Linq.JValue上的子值。”} 现在我不知道为什么会这样。我已经检查了好几次父母教育,拼写等等 public String GetWeather() { StringBuilder theWebAddress = new StringBuilder(); theWebAddress.Append("https://q

所以我试图从Yahoo的weather Json中获取天气信息,但问题是我一直得到这个错误

{“无法访问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;
    }

指向API的链接是。因此,据我所见,获取查询或结果的子级存在问题。有什么想法吗?提前谢谢。

我通过更改代码解决了这个问题。我没有使用那个代码,而是用这个

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;
        }
    }

现在它可以正常工作了。

query.results.channel不是数组。为什么不使用
ToString()
来记录
dataObject
并查看它是什么。所显示的代码不会产生所显示的错误。当我尝试它时,我得到一个
InvalidCastException
,因为
dataObject[“query”][“results”][“channel”]
不是数组。您具体想从JSON中检索哪些信息?只是当前状态文本?奇怪。。。我知道我刚才说的那个错误。是的,现在我只想要条件。