Question

I've deserialized the Json shown below using a JObject(o) from Json.Net. I've had some success addressing attributes, for example, o["items"][0]["formattedURL"] which works fine. However, I'm having trouble with something like o["queries"]["nextPage"]["startIndex"] which I'd like to return '11' but it throws an error. I've looked extensively for documentation on this but no luck.

{
 "kind": "customsearch#search",
 "url": {
  "type": "application/json",
  "template": "https://www.googleapis.com/customsearch/v1?q={searchTerms}&num={count?}&start={startIndex?}&lr={language?}&safe={safe?}&cx={cx?}&cref={cref?}&sort={sort?}&filter={filter?}&gl={gl?}&cr={cr?}&googlehost={googleHost?}&c2coff={disableCnTwTranslation?}&hq={hq?}&hl={hl?}&siteSearch={siteSearch?}&siteSearchFilter={siteSearchFilter?}&exactTerms={exactTerms?}&excludeTerms={excludeTerms?}&linkSite={linkSite?}&orTerms={orTerms?}&relatedSite={relatedSite?}&dateRestrict={dateRestrict?}&lowRange={lowRange?}&highRange={highRange?}&searchType={searchType}&fileType={fileType?}&rights={rights?}&imgSize={imgSize?}&imgType={imgType?}&imgColorType={imgColorType?}&imgDominantColor={imgDominantColor?}&alt=json"
 },
 "queries": {
  "nextPage": [
   {
    "title": "Google Custom Search - Action Motivation, Inc.",
    "totalResults": "1980000",
    "searchTerms": "Action Motivation, Inc.",
    "count": 10,
    "startIndex": 11,
    "inputEncoding": "utf8",
    "outputEncoding": "utf8",
    "safe": "off",
    "cx": {cxkey},
   }
  ],
.
.
.
  {
   "kind": "customsearch#result",
   "title": "Action Motivation, Inc.",
   "htmlTitle": "\u003cb\u003eAction Motivation, Inc\u003c/b\u003e.",
   "link": "http://www.amotive.com/",
   "displayLink": "www.amotive.com",
   "snippet": "Provides corporate and special event planning and conference management \nservices. South San Francisco, CA.",
   "htmlSnippet": "Provides corporate and special event planning and conference management \u003cbr\u003e\nservices. South San Francisco, CA.",
   "cacheId": "CVRUP-rsFioJ",
   "formattedUrl": "www.amotive.com/",
   "htmlFormattedUrl": "www.amotive.com/",
   "pagemap": {
    "metatags": [
     {
      "rating": "general",
      "resource-type": "document",
      "msvalidate.01": "4FA3F444F3F4ABC8052E636B39D4D01B"
     }
    ]
   }
  },
Was it helpful?

Solution

Looking upon the JSON, nextPage is an array but tried to invoke like an object and therefore it thrown an error.

Do like below

o["queries"]["nextPage"][0]["startIndex"] 
                 |       |____index of the array
                 |____________ Array type
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top