2013-02-01 45 views
3

我正在使用C#編寫一個項目,在這個項目中,我想輸入一個搜索詞,點擊搜索按鈕,然後將部分響應從Google複製到一個數組中,這樣我就可以遍歷它們。如何使用C#和Json.NET解析Google的JSON API搜索結果?

使用其基於JSON的API搜索谷歌是很容易

var client = new HttpClient(); 
var address = new Uri("https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=" + term); 

HttpResponseMessage response = await client.GetAsync(address); 
String stream = await response.Content.ReadAsStringAsync(); 

這將返回一個JSON字符串類似下面的(結果爲「測試搜索」一詞)

{ 
    "responseData":{ 
     "results":[ 
      { 
       "GsearchResultClass":"GwebSearch", 
       "unescapedUrl":"http://en.wikipedia.org/wiki/Wikipedia:Search_engine_test", 
       "url":"http://en.wikipedia.org/wiki/Wikipedia:Search_engine_test", 
       "visibleUrl":"en.wikipedia.org", 
       "cacheUrl":"http://www.google.com/search?q\u003dcache:g6KEStELS_MJ:en.wikipedia.org", 
       "title":"Wikipedia:\u003cb\u003eSearch\u003c/b\u003eengine\u003cb\u003etest\u003c/b\u003e-Wikipedia,thefreeencyclopedia", 
       "titleNoFormatting":"Wikipedia:Searchenginetest-Wikipedia,thefreeencyclopedia", 
       "content":"A\u003cb\u003esearch\u003c/b\u003eengine\u003cb\u003etest\u003c/b\u003ecannothelpyouavoidtheworkofinterpretingyourresultsanddecidingwhattheyreallyshow.Appearanceinanindexaloneisnotusually\u003cb\u003e...\u003c/b\u003e" 
      }, 
      { 
       "GsearchResultClass":"GwebSearch", 
       "unescapedUrl":"http://techcrunch.com/2008/07/16/google-continues-to-test-a-search-interface-that-looks-more-like-digg-every-day/", 
       "url":"http://techcrunch.com/2008/07/16/google-continues-to-test-a-search-interface-that-looks-more-like-digg-every-day/", 
       "visibleUrl":"techcrunch.com", 
       "cacheUrl":"http://www.google.com/search?q\u003dcache:r2laSUVQw8kJ:techcrunch.com", 
       "title":"GoogleContinuesTo\u003cb\u003eTest\u003c/b\u003eA\u003cb\u003eSearch\u003c/b\u003eInterfaceThatLooksMoreLike\u003cb\u003e...\u003c/b\u003e", 
       "titleNoFormatting":"GoogleContinuesToTestASearchInterfaceThatLooksMoreLike...", 
       "content":"Jul16,2008\u003cb\u003e...\u003c/b\u003eAcoupleofdaysagowepostedscreenshotsofanew\u003cb\u003esearch\u003c/b\u003einterfacebeingbucket\u003cb\u003etested\u003c/b\u003ebyGooglethatletsusersvoteupordownon\u003cb\u003e...\u003c/b\u003e" 
      }, 
      { 
       "GsearchResultClass":"GwebSearch", 
       "unescapedUrl":"http://googleblog.blogspot.com/2006/04/this-is-test-this-is-only-test.html", 
       "url":"http://googleblog.blogspot.com/2006/04/this-is-test-this-is-only-test.html", 
       "visibleUrl":"googleblog.blogspot.com", 
       "cacheUrl":"http://www.google.com/search?q\u003dcache:Ozl1cQzRT0IJ:googleblog.blogspot.com", 
       "title":"Thisisa\u003cb\u003etest\u003c/b\u003e.Thisisonlya\u003cb\u003etest\u003c/b\u003e.|OfficialGoogleBlog", 
       "titleNoFormatting":"Thisisatest.Thisisonlyatest.|OfficialGoogleBlog", 
       "content":"Apr24,2006\u003cb\u003e...\u003c/b\u003eFromtimetotime,werunliveexperimentsonGoogle—\u003cb\u003etests\u003c/b\u003evisibletoarelativelyfewpeople--todiscoverbetterwaysto\u003cb\u003esearch\u003c/b\u003e.Wedothis\u003cb\u003e...\u003c/b\u003e" 
      }, 
      { 
       "GsearchResultClass":"GwebSearch", 
       "unescapedUrl":"http://alistapart.com/article/testing-search-for-relevancy-and-precision", 
       "url":"http://alistapart.com/article/testing-search-for-relevancy-and-precision", 
       "visibleUrl":"alistapart.com", 
       "cacheUrl":"http://www.google.com/search?q\u003dcache:02Sjrd5mb0YJ:alistapart.com", 
       "title":"\u003cb\u003eTestingSearch\u003c/b\u003eforRelevancyandPrecision·AnAListApartArticle", 
       "titleNoFormatting":"TestingSearchforRelevancyandPrecision·AnAListApartArticle", 
       "content":"Sep22,2009\u003cb\u003e...\u003c/b\u003eDespitethefactthatsite\u003cb\u003esearch\u003c/b\u003eoftenreceivesthemosttraffic,it\u0026#39;salsotheplacewheretheuserexperiencedesignerbearstheleastinfluence." 
      } 
      ], 
      "cursor":{ 
      "resultCount":"1,010,000,000", 
      "pages":[ 
       { 
        "start":"0", 
        "label":1 
       }, 
       { 
        "start":"4", 
        "label":2 
       }, 
       { 
        "start":"8", 
        "label":3 
       }, 
       { 
        "start":"12", 
        "label":4 
       }, 
       { 
        "start":"16", 
        "label":5 
       }, 
       { 
        "start":"20", 
        "label":6 
       }, 
       { 
        "start":"24", 
        "label":7 
       }, 
       { 
        "start":"28", 
        "label":8 
       } 
      ], 
      "estimatedResultCount":"1010000000", 
      "currentPageIndex":0, 
      "moreResultsUrl":"http://www.google.com/search?oe\u003dutf8\u0026ie\u003dutf8\u0026source\u003duds\u0026start\u003d0\u0026hl\u003den\u0026q\u003dTest+search", 
      "searchResultTime":"0.23" 
      } 
     }, 
     "responseDetails":null, 
     "responseStatus":200 
    } 

怎麼辦我得到每個節點中的url值推入到一個數組,所以我可以遍歷它?

+0

「的谷歌網絡搜索API不再可用,請遷移到谷歌自定義搜索API (https://developers.google.com/custom-search/)「 – Max

回答

1

您可以使用關鍵字dynamic與Json.Net

dynamic jObj = JsonConvert.DeserializeObject(json); 
foreach (var res in jObj.responseData.results) 
{ 
    Console.WriteLine("{0} => {1}\n",res.title,res.url); 
} 

您可以使用LINQ太

var jObj = (JObject)JsonConvert.DeserializeObject(json); 
string[] urls = jObj["responseData"]["results"] 
       .Select(x => (string)x["url"]) 
       .ToArray(); 
+0

我嘗試使用'動態'關鍵字方法thingy,然後而不是Console.WriteLine我創建了一個列表(在循環之前)'列表線程=新列表();'(我正在使用我t搜索論壇主題,因此名單的名稱)。然後我調用'threads.Add(res.url)',我得到以下錯誤:'最好的重載方法匹配'System.Collections.Generic.List .Add(string)'有一些無效參數' –

+1

@TimeSheep嘗試'threads.Add(res.url.ToString())'或'threads.Add((string)res.url)' – I4V

+0

啊哈,謝謝,鑄造到字符串解決了問題:) –