2013-08-01 66 views
4

是否有任何庫/包返回響應於http請求而收到的json字符串。這非常簡單,所以我可以編寫自己的代碼,但是更喜歡現有的/經過測試的代碼,而不是重新發明輪子。golang - 從http請求返回json字符串的庫/包

目前,我有:

func getJsonStr(url string) ([]byte, error) { 
    resp, err := http.Get(url) 
    if err != nil { 
     return []byte{0}, err 
    } 
    defer resp.Body.Close() 
    body, err := ioutil.ReadAll(resp.Body) 
    if err != nil { 
     return []byte{0}, err 
    } 
    return body, nil 
} 

編輯:我正在尋找類似節點的「請求」模塊,它可以讓我做在同一行,像這樣:jsonStr,ERR:= getJsonStr( URL)。

+0

順便說一句,使用'return nil,err'而不是你擁有的分配一個元素片的方式,無論如何你都不應該看到(如果你真的想要一個非零但空的片,它將是'[] byte {}')。如果你想要一個班輪,那麼只需在某處寫上述函數,並使用你需要的任何一行。 –

回答

8

沒有什麼它使用現有的系統封裝,它相當簡單的go

package main 

import (
    "encoding/json" 
    "fmt" 
    "net/http" 
) 

func main() { 
    var data struct { 
     Items []struct { 
      Name    string 
      Count    int 
      Is_required  bool 
      Is_moderator_only bool 
      Has_synonyms  bool 
     } 
    } 

    r, _ := http.Get("https://api.stackexchange.com/2.2/tags?page=1&pagesize=100&order=desc&sort=popular&site=stackoverflow") 
    defer r.Body.Close() 

    dec := json.NewDecoder(r.Body) 
    dec.Decode(&data) 

    for _, item := range data.Items { 
     fmt.Printf("%s = %d\n", item.Name, item.Count) 
    } 

} 

輸出go run main.go

java = 781454 
javascript = 769128 
c# = 744294 
php = 692360 
android = 617892 
jquery = 570461 
python = 379608 
html = 374617 
c++ = 341533 
ios = 300992 
mysql = 296223 
css = 276063 
sql = 258178 
asp.net = 244833 
objective-c = 215095 
.net = 202087 
iphone = 199749 
ruby-on-rails = 190497 
c = 166226 
ruby = 123840 
sql-server = 119341 
arrays = 116975 
ajax = 109786 
regex = 107215 
xml = 106705 
asp.net-mvc = 101921 
json = 101545 
wpf = 96016 
linux = 92266 
django = 88008 
database = 86638 
eclipse = 79952 
vb.net = 78888 
r = 78401 
xcode = 76326 
windows = 74359 
angularjs = 73266 
string = 71149 
html5 = 68094 
node.js = 66731 
wordpress = 65125 
multithreading = 64168 
facebook = 62460 
excel = 58402 
spring = 57380 
image = 56798 
winforms = 55268 
forms = 54263 
ruby-on-rails-3 = 52611 
osx = 51292 
oracle = 50876 
git = 50019 
performance = 48867 
swing = 48768 
algorithm = 48258 
apache = 47355 
bash = 46347 
linq = 45169 
visual-studio-2010 = 43907 
entity-framework = 43776 
perl = 42740 
web-services = 42609 
matlab = 42112 
hibernate = 41410 
visual-studio = 40307 
wcf = 39948 
sql-server-2008 = 39394 
mongodb = 38632 
asp.net-mvc-3 = 38018 
list = 37914 
qt = 37505 
.htaccess = 37305 
css3 = 36505 
vba = 36368 
sqlite = 36274 
actionscript-3 = 35594 
file = 35171 
twitter-bootstrap = 34911 
postgresql = 34757 
function = 34747 
codeigniter = 33439 
api = 32964 
class = 32888 
scala = 32763 
shell = 32407 
google-maps = 31961 
cocoa = 31744 
ipad = 31600 
jsp = 31510 
cocoa-touch = 30994 
tsql = 30976 
sockets = 30635 
flash = 30555 
jquery-ui = 30506 
asp.net-mvc-4 = 30278 
validation = 30166 
security = 30035 
delphi = 29758 
unit-testing = 29717 
rest = 29475 
+0

是的,我可以做到這一點,但有兩件事:1。我不想每次想要得到一個JSON都輸出。我想要一個單線程,如:jsonStr,err:= jsonStr.get(url)。 2.我不想每次獲取json時都要定義一個結構體。我從不同的來源獲得它,並且更容易獲得一個字符串並將其放在jsbeautifier中。 – tldr

+0

您可以使用'github.com/bitly/go-simplejson'使用相當簡單 – Baba

+2

或者只需編寫一個接受URL的函數即可完成所有這些,非常簡單。 – Stasis

11

檢索過httpjson數據既然你不想定義錯誤結構,它也可能(但有點醜陋)解組成map map [string]接口的映射{} {}

package main 

import (
    "encoding/json" 
    "fmt" 
    "io/ioutil" 
    "net/http" 
) 

func main() { 

    data := map[string]interface{}{} 

    r, _ := http.Get("http://api.stackoverflow.com/1.1/tags?pagesize=100&page=1") 
    defer r.Body.Close() 

    body, _ := ioutil.ReadAll(r.Body) 
    json.Unmarshal(body, &data) 

    fmt.Println("Total:", data["total"], "page:", data["page"], "pagesize:", data["pagesize"]) 
    // Total: 34055 page: 1 pagesize: 100 
}