golang - 从http请求返回json字符串的库/包

时间:2013-08-01 15:57:53

标签: json http go

是否有任何库/包返回为响应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
}
编辑:我正在寻找类似Node的'request'模块,它可以让我在一行中完成,如下所示:jsonStr,err:= getJsonStr(url)。

2 个答案:

答案 0 :(得分:10)

由于您不想定义结构,因此也可以(但有点难看)将地图解组为map [string] interface {} {}

类型的地图
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
}

答案 1 :(得分:7)

使用现有系统包没有任何错误,在json

中检索http 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