无法使用GoLang的get方法获取正确的JSON响应。

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

Cannot get correct json response using GoLang get

问题

我是你的中文翻译助手,以下是你要翻译的内容:

我刚开始学习Go语言,尝试编写一个简单的网络爬虫。我正在使用DuckDuckGo的API,并尝试显示搜索结果。

这是我的代码:

package main

import (
	"fmt"
	"net/http"
)


func main() {
	getDuckDuckGo("food")
}

func getDuckDuckGo(keyword string) <- chan string{
	resp, _ := http.Get("http://api.duckduckgo.com/?q=" + keyword + "&format=json&pretty=1")
	c := make(chan string)


	fmt.Println(resp)
	var respMap map[string]interface{}
	fmt.Println(respMap)


	fmt.Println(respMap)
	return c
}

我的resp println输出的结果是:

&{200 OK 200 HTTP/1.1 1 1 map[Connection:[keep-alive] Content-Type:[application/x-javascript] Date:[Sat, 20 Dec 2014 00:41:49 GMT] Cache-Control:[max-age=1] Expires:[Sat, 20 Dec 2014 00:41:50 GMT] Server:[nginx] X-Duckduckgo-Locale:[en_US]] 0xf840053c20 -1 [chunked] false map[] 0xf84007c000}

而不是任何JSON数据。

我是否正确地发送了GET请求?

英文:

I'm new to Go and trying to write a simple web crawler. I'm using duck duck go's api and trying to display search results.

https://duckduckgo.com/api

This is my code -
package main

import (
	&quot;fmt&quot;
	&quot;net/http&quot;
)


func main() {
	getDuckDuckGo(&quot;food&quot;)
}

func getDuckDuckGo(keyword string) &lt;- chan string{
	resp, _ := http.Get(&quot;http://api.duckduckgo.com/?q=&quot; + keyword + &quot;&amp;format=json&amp;pretty=1&quot;)
	c := make(chan string)


	fmt.Println(resp)
	var respMap map[string]interface{}
	fmt.Println(respMap)


	fmt.Println(respMap)
	return c
}

My resp println gives me this -

&amp;{200 OK 200 HTTP/1.1 1 1 map[Connection:[keep-alive] Content-Type:[application/x-javascript] Date:[Sat, 20 Dec 2014 00:41:49 GMT] Cache-Control:[max-age=1] Expires:[Sat, 20 Dec 2014 00:41:50 GMT] Server:[nginx] X-Duckduckgo-Locale:[en_US]] 0xf840053c20 -1 [chunked] false map[] 0xf84007c000}

Rather than any json.

Am I doing the GET request correctly?

答案1

得分: 1

至少,你应该做以下几件事情:

  1. 检查http.Get()的错误。
  2. 通过resp.Body获取io.Reader以获取HTTP响应体数据。
  3. 使用json.Decoder解码JSON数据。

你的getDuckDuckGo()函数应该像这样改写:

func getDuckDuckGoImproved(k string) (map[string]interface{}, error) {
    resp, err := http.Get("http://api.duckduckgo.com/?=&" + k + "&format=json")
    if err != nil {
        return nil, err
    }
    defer resp.Body.Close()

    r := make(map[string]interface{})
    d := json.NewDecoder(resp.Body)
    if err := d.Decode(&r); err != nil {
        return nil, err
    }
    return r, nil
}

以上是翻译好的内容,请确认是否满意。

英文:

At least, you should do below things:

  1. Check error of http.Get()
  2. Get io.Reader by resp.Body for HTTP body data
  3. Use json.Decoder to decode json

Your getDuckDuckGo() should be became like this:

func getDuckDuckGoImproved(k string) (map[string]interface{}, error) {
    resp, err := http.Get(&quot;http://api.duckduckgo.com/?=&quot; + k + &quot;&amp;format=json&quot;)
    if err != nil {
        return nil, err
    }
    defer resp.Body.Close()

    r := make(map[string]interface{})
    d := json.NewDecoder(resp.Body)
    if err := d.Decode(&amp;r); err != nil {
        return nil, err
    }
    return r, nil
}

huangapple
  • 本文由 发表于 2014年12月20日 08:48:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/27575904.html
匿名

发表评论

匿名网友

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

确定