Unable to access child value in Newtonsoft.Json.Linq.JValue

I am working on WinForms (C #) to find rank and keyword positions in Google and Bing. For this, I use Newtonsoft.Json.Net2.0.dll . While I start the process, it shows an error:

Unable to access child value in Newtonsoft.Json.Linq.JValue.

How can I solve this problem?

 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; } } 
+4
source share
1 answer

Most likely, this is due to the fact that the NewtonSoft library is trying to associate an object with nonexistent properties. In line

SearchType searchResult = JsonConvert.DeserializeObject<SearchType>(result.ToString());

deserialization may fail due to a result containing properties not found in SearchType.

+2
source

All Articles