gpt4 book ai didi

c# - 如何查看 JEnumerable 对象中的 Children().ToList() 方法?

转载 作者:太空狗 更新时间:2023-10-30 00:55:59 25 4
gpt4 key购买 nike

我尝试通过 JSON.NET 解析 JSON,在 App 类中没问题,但如果我想在自定义类中编写解析方法,VS 看不到方法 ToList()JEnumerable<JToken>对象。

list :

JObject newsResponse = JObject.Parse(e.Result);
IList<JToken> results = newsResponse["NewsResponse"]["NewsItems"]["NewsItem"].Children().ToList(); //Here don't see ToList()
List<News> newsResults = new List<News>();

foreach (JToken result in results)
{
News searchResult = JsonConvert.DeserializeObject<News>(result.ToString());
newsResults.Add(searchResult);
}

错误:

'Newtonsoft.Json.Linq.JEnumerable' does not contain a definition for 'ToList' and no extension method 'ToList' accepting a first argument of type 'Newtonsoft.Json.Linq.JEnumerable' could be found (are you missing a using directive or an assembly reference?)

最佳答案

你不需要在这里使用ToList()

var results = newsResponse["NewsResponse"]["NewsItems"]["NewsItem"].Children(); 

这将生成一个可与下面的 foreach 一起使用的可枚举(惰性)集合。

注意:我使用 var 是因为我不知道这里返回的确切类型,但我们并不关心它,它可能非常复杂,所以它非常有用。

关于c# - 如何查看 JEnumerable<JToken> 对象中的 Children().ToList() 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8832662/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com