访问反序列化的JObject(Json.Net)中的属性

时间:2014-05-05 04:20:37

标签: c# json json.net

我使用Json.Net中的JObject(o)对下面显示的Json进行了反序列化。我在寻址属性方面取得了一些成功,例如,o [“items”] [0] [“formattedURL”]工作正常。但是,我遇到类似o [“queries”] [“nextPage”] [“startIndex”]之类的问题,我想返回“11”,但它会抛出错误。我已经广泛关注这方面的文档,但没有运气。

{
 "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"
     }
    ]
   }
  },

1 个答案:

答案 0 :(得分:1)

在查看JSON时, nextPage 是一个数组但是试图像对象一样调用,因此它会抛出错误。

如下所示

o["queries"]["nextPage"][0]["startIndex"] 
                 |       |____index of the array
                 |____________ Array type