使用Golang从DuckDuckGo中解码JSON

时间:2013-11-23 00:10:22

标签: json go duckduckgo-api

我一直在使用Go library for DuckDuckGo,除了一件事以外,它一直很好用。在“RelatedTopics”部分。通常,它看起来像这样:

{
"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(这是图书馆使用的)来做这件事。

此致

生姜比尔

1 个答案:

答案 0 :(得分: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
}