本文整理汇总了Golang中encoding/json.NewDecoder函数的典型用法代码### 示例。如果您正苦于以下问题:Golang NewDecoder函数的具体用法?Golang NewDecoder怎么用?Golang NewDecoder使用的例子?那么恭喜您, 这里精选的函数代码### 示例或许可以为您提供帮助。

在下文中一共展示了NewDecoder函数的20个代码### 示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码### 示例。

示例1: TestParseFlavors

func TestParseFlavors(t *testing.T) {

    flavorsTestFilePath := "./testdata/database_flavor_test_flavors.json"

    flavorsTestFile, err := os.Open("./testdata/database_flavor_test_flavors.json")
    if err != nil {
        t.Error(fmt.Errorf("Failed to open file %s: '%s'", flavorsTestFilePath, err.Error()))
    }

    flavorsContainer := FlavorsContainer{}
    err = json.NewDecoder(flavorsTestFile).Decode(&flavorsContainer)
    defer flavorsTestFile.Close()
    if err != nil {
        t.Error(fmt.Errorf("Failed to decode JSON file %s: '%s'", flavorsTestFilePath, err.Error()))
    }

    flavorTestFilePath := "./testdata/database_flavor_test_flavor.json"

    flavorTestFile, err := os.Open(flavorTestFilePath)
    if err != nil {
        t.Error(fmt.Errorf("Failed to open file %s: '%s'", flavorTestFilePath, err.Error()))
    }

    flavorContainer := FlavorContainer{}
    err = json.NewDecoder(flavorTestFile).Decode(&flavorContainer)
    defer flavorTestFile.Close()
    if err != nil {
        t.Error(fmt.Errorf("Failed to decode JSON file %s: '%s'", flavorTestFilePath, err.Error()))
    }

    return
}

开发者ID:alvaropeon,项目名称:hpcloud-kubesetup,代码行数:32,代码来源:flavor_test.go

示例2: Handler

func Handler(response http.ResponseWriter, request *http.Request) {
    fmt.Println(request.Method)
    fmt.Println("Entrou no handler")
    fmt.Println(len(request.Cookies()))
    token, err := request.Cookie("token")
    fmt.Println(token.Value, err)
    data := url.Values{}
    data.Set("access_token", token.Value)
    //req = http.NewRequest("POST", "https://www.googleapis.com/oauth2/v1/tokeninfo", nil)
    resp, err := http.Post("https://www.googleapis.com/oauth2/v1/tokeninfo", "application/x-www-form-urlencoded", bytes.NewBufferString(data.Encode()))
    //req.Header.Set("Content-Type", "application/x-www-form-urlencoded; param="+token)
    //http.
    var retorno ValidaToken
    json.NewDecoder(resp.Body).Decode(&retorno)
    fmt.Println(retorno.Error, retorno.Email)

    switch request.Method {
    case "GET":
        provas := Provas()
        fmt.Println(provas)
        js, _ := json.MarshalIndent(provas, " ", "   ")
        response.Write(js)

    case "POST":
        var prova Prova
        json.NewDecoder(request.Body).Decode(&prova)
        err := Insere(&prova)
        fmt.Println(err)
    }
}

开发者ID:GrupoEstudoMega,项目名称:MegaRunning,代码行数:30,代码来源:prova.go

示例3: Do

func (c *Client) Do(req *http.Request, v interface{}) (*http.Response, error) {
    resp, err := c.client.Do(req)
    if err != nil {
        return nil, err
    }

    defer resp.Body.Close()

    if resp.StatusCode > 299 {
        var errObject Error
        if err := json.NewDecoder(resp.Body).Decode(&errObject); err != nil {
            return resp, &ErrAPI{Response: resp}
        }

        return resp, &ErrAPI{
            Response: resp,
            Err:      &errObject,
        }
    }

    if v != nil {
        err = json.NewDecoder(resp.Body).Decode(v)
    }

    return resp, err
}

开发者ID:jkotrlik,项目名称:salsaflow,代码行数:26,代码来源:client.go

示例4: poll

// Poll polls the Laika API for the latest Feature statuses, storing the results
// in the internal cache.
func (c *client) poll() error {
    req, err := http.NewRequest("GET", c.url.String(), nil)
    if err != nil {
        return err
    }

    req.SetBasicAuth(c.username, c.password)
    req.Header.Set("Content-Type", "application/json")

    res, err := http.DefaultClient.Do(req)
    if err != nil {
        return err
    }

    defer res.Body.Close()

    if res.StatusCode < 200 || res.StatusCode >= 300 {
        e := new(Error)
        if err := json.NewDecoder(res.Body).Decode(e); err != nil {
            return err
        }

        return errors.New(e.Message)
    }

    features := []*Feature{}
    if err := json.NewDecoder(res.Body).Decode(&features); err != nil {
        return err
    }

    c.features.AddAll(features)

    return nil
}

开发者ID:medigo,项目名称:laika,代码行数:36,代码来源:client.go

示例5: handleResponse

// handleResponse returns a filled in *oauth.Token given the *http.Response from
// a *http.Request created by buildRequest.
func handleResponse(r *http.Response) (*oauth.Token, error) {
    o := &oauth.Token{}
    defer r.Body.Close()
    if r.StatusCode != 200 {
        return o, errors.New("invalid response: " + r.Status)
    }
    b := &respBody{}
    err := json.NewDecoder(r.Body).Decode(b)
    if err != nil {
        return o, err
    }
    o.AccessToken = b.Access
    if b.IdToken != "" {
        // decode returned id token to get expiry
        o.AccessToken = b.IdToken
        s := strings.Split(b.IdToken, ".")
        if len(s) < 2 {
            return nil, errors.New("invalid token received")
        }
        d, err := base64Decode(s[1])
        if err != nil {
            return o, err
        }
        c := &ClaimSet{}
        err = json.NewDecoder(bytes.NewBuffer(d)).Decode(c)
        if err != nil {
            return o, err
        }
        o.Expiry = time.Unix(c.Exp, 0)
        return o, nil
    }
    o.Expiry = time.Now().Add(b.ExpiresIn * time.Second)
    return o, nil
}

开发者ID:BitBalloon,项目名称:bitballoon-cli,代码行数:36,代码来源:jwt.go

最后编辑: kuteng  文档更新时间: 2021-08-23 19:14   作者:kuteng