c# – 无法访问Newtonsoft.Json.Linq.JValue上的子值
发布时间:2020-12-15 17:16:29 所属栏目:百科 来源:网络整理
导读:我正在使用WinForms(C#)来查找Google和Bing中的排名和关键字位置.为此,我使用的是Newtonsoft.Json.Net2.0.dll.当我正在运行该过程时,它显示错误: Cannot access child value on Newtonsoft.Json.Linq.JValue. 我怎么解决这个问题? public class GoogleSear
我正在使用WinForms(C#)来查找Google和Bing中的排名和关键字位置.为此,我使用的是Newtonsoft.Json.Net2.0.dll.当我正在运行该过程时,它显示错误:
我怎么解决这个问题? public class GoogleSearch { public int Search(string siteUrl,string searchExpression,ref string stage) { int position = 100; const string urlTemplate = @"http://ajax.googleapis.com/ajax/services/search/web?v=1.0&rsz=large&safe=active&q={0}&start={1}"; var resultsList = new List<SearchType>(); int[] offsets = { 0,8,16,24,32,40,48 }; foreach (var offset in offsets) { var searchUrl = new Uri(string.Format(urlTemplate,searchExpression,offset)); string page = new WebClient().DownloadString(searchUrl); JObject googleSearch = JObject.Parse(page); IList<JToken> results = googleSearch["responseData"]["results"].Children().ToList();//here i got the error ... IList<SearchType> searchResults = new List<SearchType>(); foreach (JToken result in results) { SearchType searchResult = JsonConvert.DeserializeObject<SearchType>(result.ToString()); resultsList.Add(searchResult); } } int i = 0; foreach (SearchType s in resultsList) { i = i + 1; if (s.Url.Contains(siteUrl)) { position = i; return position; } } return position; } } 解决方法
这很可能是由于NewtonSoft库试图将对象绑定到不存在的属性.在线
SearchType searchResult = JsonConvert.DeserializeObject< SearchType>(result.ToString()); 由于结果包含SearchType中不存在的属性,反序列化可能会失败. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |