2013-11-23 45 views
0

我一直在使用Go library for DuckDuckGo,它除了一件事以外一直在正常工作。在「RelatedTopics」部分。通常情況下,它應該是這樣的:使用Golang從DuckDuckGo解碼JSON

{ 
"RelatedTopics" : [ 
     { 
     "Result" : "<a href=\"http://duckduckgo.com/Criticism_of_Google\">Criticism of Google</a> - Criticism of Google includes possible misuse and manipulation of search results, its use of others' intellectual property, concerns that its compilation of data may violate people's privacy, cen...", 
     "Icon" : { 
      "URL" : "", 
      "Height" : "", 
      "Width" : "" 
     }, 
     "FirstURL" : "http://duckduckgo.com/Criticism_of_Google", 
     "Text" : "Criticism of Google - Criticism of Google includes possible misuse and manipulation of search results, its use of others' intellectual property, concerns that its compilation of data may violate people's privacy, cen..." 
     }, 
     { 
     "Result" : "<a href=\"http://duckduckgo.com/PRISM_(surveillance_program)\">PRISM</a> - PRISM is a clandestine mass electronic surveillance data mining program known to have been operated by the United States National Security Agency (NSA) since 2007.", 
     "Icon" : { 
      "URL" : "", 
      "Height" : "", 
      "Width" : "" 
     }, 
     "FirstURL" : "http://duckduckgo.com/PRISM_(surveillance_program)", 
     "Text" : "PRISM - PRISM is a clandestine mass electronic surveillance data mining program known to have been operated by the United States National Security Agency (NSA) since 2007." 
     }, 
     { 
     "Result" : "<a href=\"http://duckduckgo.com/Google_Panda\">Google Panda</a> - Google Panda is a change to Google's search results ranking algorithm that was first released in February 2011.", 
     "Icon" : { 
      "URL" : "", 
      "Height" : "", 
      "Width" : "" 
     }, 
     "FirstURL" : "http://duckduckgo.com/Google_Panda", 
     "Text" : "Google Panda - Google Panda is a change to Google's search results ranking algorithm that was first released in February 2011." 
     }, 
     { 
     "Result" : "<a href=\"http://duckduckgo.com/Google_Penguin\">Google Penguin</a> - Google Penguin is a code name for a Google algorithm update that was first announced on April 24, 2012.", 
     "Icon" : { 
      "URL" : "", 
      "Height" : "", 
      "Width" : "" 
     }, 
     "FirstURL" : "http://duckduckgo.com/Google_Penguin", 
     "Text" : "Google Penguin - Google Penguin is a code name for a Google algorithm update that was first announced on April 24, 2012." 
     }, 
     { 
     "Result" : "<a href=\"http://duckduckgo.com/Google_search_features\">Google search features</a> - The Google search features include more than 40 options or keywords to modify the type of search.", 
     "Icon" : { 
      "URL" : "", 
      "Height" : "", 
      "Width" : "" 
     }, 
     "FirstURL" : "http://duckduckgo.com/Google_search_features", 
     "Text" : "Google search features - The Google search features include more than 40 options or keywords to modify the type of search." 
     }, 
     { 
     "Result" : "<a href=\"http://duckduckgo.com/Google_Searchology\">Google Searchology</a> - Google Searchology 2009 witnessed several novel changes in the Google search options.", 
     "Icon" : { 
      "URL" : "", 
      "Height" : "", 
      "Width" : "" 
     }, 
     "FirstURL" : "http://duckduckgo.com/Google_Searchology", 
     "Text" : "Google Searchology - Google Searchology 2009 witnessed several novel changes in the Google search options." 
     }, 
     { 
     "Result" : "<a href=\"http://duckduckgo.com/c/Multilingual_websites\">Multilingual websites</a>", 
     "Icon" : { 
      "URL" : "", 
      "Height" : "", 
      "Width" : "" 
     }, 
     "FirstURL" : "http://duckduckgo.com/c/Multilingual_websites", 
     "Text" : "Multilingual websites" 
     }, 
     { 
     "Result" : "<a href=\"http://duckduckgo.com/c/Google_services\">Google services</a>", 
     "Icon" : { 
      "URL" : "", 
      "Height" : "", 
      "Width" : "" 
     }, 
     "FirstURL" : "http://duckduckgo.com/c/Google_services", 
     "Text" : "Google services" 
     }, 
     { 
     "Result" : "<a href=\"http://duckduckgo.com/c/Internet_search_engines\">Internet search engines</a>", 
     "Icon" : { 
      "URL" : "", 
      "Height" : "", 
      "Width" : "" 
     }, 
     "FirstURL" : "http://duckduckgo.com/c/Internet_search_engines", 
     "Text" : "Internet search engines" 
     }, 
     { 
     "Result" : "<a href=\"http://duckduckgo.com/c/American_websites\">American websites</a>", 
     "Icon" : { 
      "URL" : "", 
      "Height" : "", 
      "Width" : "" 
     }, 
     "FirstURL" : "http://duckduckgo.com/c/American_websites", 
     "Text" : "American websites" 
     } 
    ] 
} 

但是,有時「RealtedTopics」一節中,一系列「主題」的,在他們衆多的「結果」和「名」這個話題的出現。

我很抱歉,如果這沒有多大意義。 A good example of this is when you search Doctor Who.

{ 
"RelatedTopics" : [ 
     { 
     "Result" : "<a href=\"http://duckduckgo.com/Doctor_Who\">Doctor Who</a> is the title of a long-running British science fiction series.", 
     "Icon" : { 
      "URL" : "https://i.duckduckgo.com/i/www.bbc.co.uk.ico", 
      "Height" : 16, 
      "Width" : 16 
     }, 
     "FirstURL" : "http://duckduckgo.com/Doctor_Who", 
     "Text" : "Doctor Who is the title of a long-running British science fiction series." 
     }, 
     { 
     "Result" : "<a href=\"http://duckduckgo.com/Doctor_(Doctor_Who)\">Doctor (Doctor Who)</a>, the main character in the British television series", 
     "Icon" : { 
      "URL" : "https://i.duckduckgo.com/i/e32c2703.jpg", 
      "Height" : "", 
      "Width" : "" 
     }, 
     "FirstURL" : "http://duckduckgo.com/Doctor_(Doctor_Who)", 
     "Text" : "Doctor (Doctor Who), the main character in the British television series" 
     }, 
     { 
     "Result" : "<a href=\"http://duckduckgo.com/Doctor_Who_theme_music\">Doctor Who theme music</a>, the theme music created for the television series, and has changed many times over the 50 years it has been running.", 
     "Icon" : { 
      "URL" : "", 
      "Height" : "", 
      "Width" : "" 
     }, 
     "FirstURL" : "http://duckduckgo.com/Doctor_Who_theme_music", 
     "Text" : "Doctor Who theme music, the theme music created for the television series, and has changed many times over the 50 years it has been running." 
     }, 
     { 
     "Topics" : [ 
      { 
       "Result" : "<a href=\"http://duckduckgo.com/Doctor_Who_(film)\">Doctor Who (film)</a>, the television movie starring Paul McGann, based on the television series", 
       "Icon" : { 
        "URL" : "", 
        "Height" : "", 
        "Width" : "" 
       }, 
       "FirstURL" : "http://duckduckgo.com/Doctor_Who_(film)", 
       "Text" : "Doctor Who (film), the television movie starring Paul McGann, based on the television series" 
      }, 
      { 
       "Result" : "<a href=\"http://duckduckgo.com/Dr._Who_(Dalek_films)\">Dr. Who (Dalek films)</a>, the human character played by Peter Cushing in two films based on the television series", 
       "Icon" : { 
        "URL" : "https://i.duckduckgo.com/i/9f10647e.jpg", 
        "Height" : "", 
        "Width" : "" 
       }, 
       "FirstURL" : "http://duckduckgo.com/Dr._Who_(Dalek_films)", 
       "Text" : "Dr. Who (Dalek films), the human character played by Peter Cushing in two films based on the television series" 
      }, 
      { 
       "Result" : "<a href=\"http://duckduckgo.com/Doctor_Who_(pinball)\">Doctor Who (pinball)</a>, pinball machine based on the television series", 
       "Icon" : { 
        "URL" : "https://i.duckduckgo.com/i/eee6de34.jpg", 
        "Height" : "", 
        "Width" : "" 
       }, 
       "FirstURL" : "http://duckduckgo.com/Doctor_Who_(pinball)", 
       "Text" : "Doctor Who (pinball), pinball machine based on the television series" 
      }, 
      { 
       "Result" : "<a href=\"http://duckduckgo.com/Doctor_Who_theme_music\">Doctor Who theme music</a>, the theme music created for the television series, and has changed many times over the 50 years it has been running.", 
       "Icon" : { 
        "URL" : "", 
        "Height" : "", 
        "Width" : "" 
       }, 
       "FirstURL" : "http://duckduckgo.com/Doctor_Who_theme_music", 
       "Text" : "Doctor Who theme music, the theme music created for the television series, and has changed many times over the 50 years it has been running." 
      }, 
      { 
       "Result" : "<a href=\"http://duckduckgo.com/Doctor_Who_(CSI)\">\"Doctor Who\" (CSI)</a>, the 22nd episode of the 10th season of CSI: Crime Scene Investigation", 
       "Icon" : { 
        "URL" : "", 
        "Height" : "", 
        "Width" : "" 
       }, 
       "FirstURL" : "http://duckduckgo.com/Doctor_Who_(CSI)", 
       "Text" : "\"Doctor Who\" (CSI), the 22nd episode of the 10th season of CSI: Crime Scene Investigation" 
      } 
     ], 
     "Name" : "In media and entertainment" 
     }, 
     { 
     "Topics" : [ 
      { 
       "Result" : "<a href=\"http://duckduckgo.com/Neoregelia_'Dr._Who'\">Neoregelia 'Dr. Who'</a>, a hybrid cultivar of the genus Neoregelia in the Bromeliad family", 
       "Icon" : { 
        "URL" : "", 
        "Height" : "", 
        "Width" : "" 
       }, 
       "FirstURL" : "http://duckduckgo.com/Neoregelia_'Dr._Who'", 
       "Text" : "Neoregelia 'Dr. Who', a hybrid cultivar of the genus Neoregelia in the Bromeliad family" 
      } 
     ], 
     "Name" : "In other uses" 
     } 
    ] 
} 

如何擴展庫以便我可以訪問這些「主題」?我想不出使用json.Unmarshal(這是什麼庫使用)來做到這一點。

問候,

姜比爾

+0

看這裏(解壓到結構)https://gobyexample.com/ json – elithrar

回答

3

這似乎像題目中就包括片Topics []Topic

type Icon struct { 
    URL string 
    Height string 
    Width string 
} 

type Topic struct { 
    Result string 
    Icon Icon 
    FirstURL string 
    Text string 
    Topics []Topic `json:",omitempty"` 
} 

type RootObj struct { 
RelatedTopics []Topic 
} 
+0

感謝您的幫助。我不得不添加一行才能正常工作。在主題之後,我添加了名稱字符串'json:「,omitempty」' – Xplane