使用Golang从DuckDuckGo解码JSON

huangapple go评论91阅读模式
英文:

Decoding JSON from the DuckDuckGo using Golang

问题

我一直在使用Go 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"
      }
   ]
}

然而,在“RelatedTopics”部分中,有时会出现一系列带有多个“Results”的“Topics”和该主题的“Name”。

如果我想要访问这些“Topics”,我该如何扩展该库?我无法想到使用json.Unmarshal(这是该库使用的方法)来实现。

问候,

Ginger Bill

英文:

I've been using the Go library for DuckDuckGo and it has been working fine except for one thing. In the "RelatedTopics" Section. Usually, it would look like this:

{
&quot;RelatedTopics&quot; : [
      {
         &quot;Result&quot; : &quot;&lt;a href=\&quot;http://duckduckgo.com/Criticism_of_Google\&quot;&gt;Criticism of Google&lt;/a&gt; - Criticism of Google includes possible misuse and manipulation of search results, its use of others&#39; intellectual property, concerns that its compilation of data may violate people&#39;s privacy, cen...&quot;,
         &quot;Icon&quot; : {
            &quot;URL&quot; : &quot;&quot;,
            &quot;Height&quot; : &quot;&quot;,
            &quot;Width&quot; : &quot;&quot;
         },
         &quot;FirstURL&quot; : &quot;http://duckduckgo.com/Criticism_of_Google&quot;,
         &quot;Text&quot; : &quot;Criticism of Google - Criticism of Google includes possible misuse and manipulation of search results, its use of others&#39; intellectual property, concerns that its compilation of data may violate people&#39;s privacy, cen...&quot;
      },
      {
         &quot;Result&quot; : &quot;&lt;a href=\&quot;http://duckduckgo.com/PRISM_(surveillance_program)\&quot;&gt;PRISM&lt;/a&gt; - 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.&quot;,
         &quot;Icon&quot; : {
            &quot;URL&quot; : &quot;&quot;,
            &quot;Height&quot; : &quot;&quot;,
            &quot;Width&quot; : &quot;&quot;
         },
         &quot;FirstURL&quot; : &quot;http://duckduckgo.com/PRISM_(surveillance_program)&quot;,
         &quot;Text&quot; : &quot;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.&quot;
      },
      {
         &quot;Result&quot; : &quot;&lt;a href=\&quot;http://duckduckgo.com/Google_Panda\&quot;&gt;Google Panda&lt;/a&gt; - Google Panda is a change to Google&#39;s search results ranking algorithm that was first released in February 2011.&quot;,
         &quot;Icon&quot; : {
            &quot;URL&quot; : &quot;&quot;,
            &quot;Height&quot; : &quot;&quot;,
            &quot;Width&quot; : &quot;&quot;
         },
         &quot;FirstURL&quot; : &quot;http://duckduckgo.com/Google_Panda&quot;,
         &quot;Text&quot; : &quot;Google Panda - Google Panda is a change to Google&#39;s search results ranking algorithm that was first released in February 2011.&quot;
      },
      {
         &quot;Result&quot; : &quot;&lt;a href=\&quot;http://duckduckgo.com/Google_Penguin\&quot;&gt;Google Penguin&lt;/a&gt; - Google Penguin is a code name for a Google algorithm update that was first announced on April 24, 2012.&quot;,
         &quot;Icon&quot; : {
            &quot;URL&quot; : &quot;&quot;,
            &quot;Height&quot; : &quot;&quot;,
            &quot;Width&quot; : &quot;&quot;
         },
         &quot;FirstURL&quot; : &quot;http://duckduckgo.com/Google_Penguin&quot;,
         &quot;Text&quot; : &quot;Google Penguin - Google Penguin is a code name for a Google algorithm update that was first announced on April 24, 2012.&quot;
      },
      {
         &quot;Result&quot; : &quot;&lt;a href=\&quot;http://duckduckgo.com/Google_search_features\&quot;&gt;Google search features&lt;/a&gt; - The Google search features include more than 40 options or keywords to modify the type of search.&quot;,
         &quot;Icon&quot; : {
            &quot;URL&quot; : &quot;&quot;,
            &quot;Height&quot; : &quot;&quot;,
            &quot;Width&quot; : &quot;&quot;
         },
         &quot;FirstURL&quot; : &quot;http://duckduckgo.com/Google_search_features&quot;,
         &quot;Text&quot; : &quot;Google search features - The Google search features include more than 40 options or keywords to modify the type of search.&quot;
      },
      {
         &quot;Result&quot; : &quot;&lt;a href=\&quot;http://duckduckgo.com/Google_Searchology\&quot;&gt;Google Searchology&lt;/a&gt; - Google Searchology 2009 witnessed several novel changes in the Google search options.&quot;,
         &quot;Icon&quot; : {
            &quot;URL&quot; : &quot;&quot;,
            &quot;Height&quot; : &quot;&quot;,
            &quot;Width&quot; : &quot;&quot;
         },
         &quot;FirstURL&quot; : &quot;http://duckduckgo.com/Google_Searchology&quot;,
         &quot;Text&quot; : &quot;Google Searchology - Google Searchology 2009 witnessed several novel changes in the Google search options.&quot;
      },
      {
         &quot;Result&quot; : &quot;&lt;a href=\&quot;http://duckduckgo.com/c/Multilingual_websites\&quot;&gt;Multilingual websites&lt;/a&gt;&quot;,
         &quot;Icon&quot; : {
            &quot;URL&quot; : &quot;&quot;,
            &quot;Height&quot; : &quot;&quot;,
            &quot;Width&quot; : &quot;&quot;
         },
         &quot;FirstURL&quot; : &quot;http://duckduckgo.com/c/Multilingual_websites&quot;,
         &quot;Text&quot; : &quot;Multilingual websites&quot;
      },
      {
         &quot;Result&quot; : &quot;&lt;a href=\&quot;http://duckduckgo.com/c/Google_services\&quot;&gt;Google services&lt;/a&gt;&quot;,
         &quot;Icon&quot; : {
            &quot;URL&quot; : &quot;&quot;,
            &quot;Height&quot; : &quot;&quot;,
            &quot;Width&quot; : &quot;&quot;
         },
         &quot;FirstURL&quot; : &quot;http://duckduckgo.com/c/Google_services&quot;,
         &quot;Text&quot; : &quot;Google services&quot;
      },
      {
         &quot;Result&quot; : &quot;&lt;a href=\&quot;http://duckduckgo.com/c/Internet_search_engines\&quot;&gt;Internet search engines&lt;/a&gt;&quot;,
         &quot;Icon&quot; : {
            &quot;URL&quot; : &quot;&quot;,
            &quot;Height&quot; : &quot;&quot;,
            &quot;Width&quot; : &quot;&quot;
         },
         &quot;FirstURL&quot; : &quot;http://duckduckgo.com/c/Internet_search_engines&quot;,
         &quot;Text&quot; : &quot;Internet search engines&quot;
      },
      {
         &quot;Result&quot; : &quot;&lt;a href=\&quot;http://duckduckgo.com/c/American_websites\&quot;&gt;American websites&lt;/a&gt;&quot;,
         &quot;Icon&quot; : {
            &quot;URL&quot; : &quot;&quot;,
            &quot;Height&quot; : &quot;&quot;,
            &quot;Width&quot; : &quot;&quot;
         },
         &quot;FirstURL&quot; : &quot;http://duckduckgo.com/c/American_websites&quot;,
         &quot;Text&quot; : &quot;American websites&quot;
      }
   ]
}

However, sometimes in the "RealtedTopics" section, series of "Topics" with numerous "Results" in them and a "Name" of that Topic appear.

I apologize if that didn't make much a sense. A good example of this is when you search Doctor Who.

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

How would I extend the library so that I could access these "Topics"? I cannot think of way using json.Unmarshal (this what the library uses) to do it.

Regards,

Ginger Bill

答案1

得分: 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
}
英文:

It seem like the Topic includes within it a slice 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:&quot;,omitempty&quot;`
}

type RootObj struct {
 RelatedTopics []Topic
}

huangapple
  • 本文由 发表于 2013年11月23日 08:10:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/20156775.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定