2014-05-05 101 views
0

我使用Json.Net中的JObject(o)反序列化了如下所示的Json。我已經成功解決了一些屬性問題,例如o [「items」] [0] [「formattedURL」],它工作正常。然而,我遇到了類似o [「queries」] [「nextPage」] [「startIndex」]的錯誤,我想返回'11',但它會引發錯誤。我已經廣泛地尋找關於此的文檔,但沒有運氣。在反序列化JObject中訪問屬性(Json.Net)

{ 
"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

尋找於JSON,nextPage是一個數組,但是設法調用等的對象,因此引發的錯誤。

執行像下面

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

這工作:串c = OJ1 [ 「查詢」] [ 「下一頁」] [0] [ 「的startIndex」]的ToString(); –

+0

Praveen:另一個問題。由於「nextPage」是一個數組,因爲這是第一個數組元素,是不是索引[0]與「startIndex」相同?我不確定[0]在做什麼;似乎多餘。 –