How to get JSON response from http.Get

后端 未结 4 995
别那么骄傲
别那么骄傲 2020-11-29 15:22

I\'m trying read JSON data from web, but that code returns empty result. I\'m not sure what I\'m doing wrong here.

package main

import \"os\"
import \"fmt\"         


        
4条回答
  •  佛祖请我去吃肉
    2020-11-29 15:53

    The ideal way is not to use ioutil.ReadAll, but rather use a decoder on the reader directly. Here's a nice function that gets a url and decodes its response onto a target structure.

    var myClient = &http.Client{Timeout: 10 * time.Second}
    
    func getJson(url string, target interface{}) error {
        r, err := myClient.Get(url)
        if err != nil {
            return err
        }
        defer r.Body.Close()
    
        return json.NewDecoder(r.Body).Decode(target)
    }
    

    Example use:

    type Foo struct {
        Bar string
    }
    
    func main() {
        foo1 := new(Foo) // or &Foo{}
        getJson("http://example.com", foo1)
        println(foo1.Bar)
    
        // alternately:
    
        foo2 := Foo{}
        getJson("http://example.com", &foo2)
        println(foo2.Bar)
    }
    

    You should not be using the default *http.Client structure in production as this answer originally demonstrated! (Which is what http.Get/etc call to). The reason is that the default client has no timeout set; if the remote server is unresponsive, you're going to have a bad day.

提交回复
热议问题