解组json响应结构为空

时间:2017-10-21 13:03:33

标签: go struct unmarshalling

我正在尝试解组JSON响应并且它是空的。我确信我在这里失踪是非常愚蠢的事情!

JSON:

{
 "response": [{
    "remain_quota_hour": 500,
    "remain_quota_month": 10000,
    "assigned_quota_hour": 500,
    "assigned_quota_month": 10000,
    "hourly_quota_next_reset": "1508464800",
    "monthly_quota_next_reset": "1509494400",
    "quota_id": "H973AA8",
    "cloud_monthly_quota_period_start": "1506816000",
    "cloud_monthly_quota_usage_for_this_gw": 0,
    "cloud_hourly_quota_usage_for_this_gw": 0,
    "cloud_monthly_quota_usage_for_quota_id": 0,
    "cloud_hourly_quota_usage_for_quota_id": 0,
    "monthly_exceeded_quota": 0,
    "hourly_exceeded_quota": 0,
    "cloud_quota_max_allow_to_exceed_percentage": 1000,
    "pod_time_gmt": "1508461217",
    "quota_expiration": "1510358400",
    "action": "ALLOW"
  }]
}

STRUCT:

type Quotas struct {
  Remain_quota_hour   int   `json:"remain_quota_hour"`
  Remain_quota_month int `json:"remain_quota_month"`
  Assigned_quota_hour int `json:"assigned_quota_hour"`
  Assigned_quota_month int `json:"assigned_quota_month"`
  Hourly_quota_next_reset string `json:"hourly_quota_next_reset"`
  Monthly_quota_next_reset string `json:"monthly_quota_next_reset"`
  Quota_id string  `json:"quota_id"`
  Cloud_monthly_quota_period_start string `json:"cloud_monthly_quota_period_start"`
  Cloud_monthly_quota_usage_for_this_gw int `json:"cloud_monthly_quota_usage_for_this_gw"`
  Cloud_hourly_quota_usage_for_this_gw int `json:"cloud_hourly_quota_usage_for_this_gw"`
  Cloud_monthly_quota_usage_for_quota_id int `json:"cloud_monthly_quota_usage_for_quota_id"`
  Cloud_hourly_quota_usage_for_quota_id int `json:"cloud_hourly_quota_usage_for_quota_id"`
  Monthly_exceeded_quota int `json:"monthly_exceeded_quota"`
  Hourly_exceeded_quota int `json:"hourly_exceeded_quota"`
  Cloud_quota_max_allow_to_exceed_percentage int `json:"cloud_quota_max_allow_to_exceed_percentage"`
  Pod_time_gmt string `json:"pod_time_gmt"`
  Quota_expiration string `json:"quota_expiration"`
  Action string `json:"action"`
}

HTTP请求和解组:

{
    httpClient := http.Client{Timeout: time.Second * 20}
    service = service + "quota"
    req, err := http.NewRequest(http.MethodGet, service, nil)
    if err != nil {
        log.Fatal(err)
    }
    req.Header.Set("Authorization", token)
    res, getErr := httpClient.Do(req)
    if getErr != nil {
        log.Fatal(getErr)
    }
    log.Println("Header")
    body, readErr := ioutil.ReadAll(res.Body)
    if readErr != nil {
        log.Fatal(readErr)
    }
    var quota1 Quotas
    jsonErr := json.Unmarshal(body, &quota1)
    if jsonErr != nil {
        log.Fatal(jsonErr)
    }
    log.Println(quota1.Action)
    return quota1.Action
}

我可以通过字符串(正文)看到JSON正在关闭,但没有分配给结构。我一度绝望,并转移到json.decoder得到相同的结果。有什么想法吗?

2 个答案:

答案 0 :(得分:3)

你有一个错误的Quotas结构定义,你可以从json有效负载看到一个数组

package main

import (
    "encoding/json"
    "fmt"
    "strings"
)

type Response struct {
    Quotas []struct {
        Remain_quota_hour                          int    `json:"remain_quota_hour"`
        Remain_quota_month                         int    `json:"remain_quota_month"`
        Assigned_quota_hour                        int    `json:"assigned_quota_hour"`
        Assigned_quota_month                       int    `json:"assigned_quota_month"`
        Hourly_quota_next_reset                    string `json:"hourly_quota_next_reset"`
        Monthly_quota_next_reset                   string `json:"monthly_quota_next_reset"`
        Quota_id                                   string `json:"quota_id"`
        Cloud_monthly_quota_period_start           string `json:"cloud_monthly_quota_period_start"`
        Cloud_monthly_quota_usage_for_this_gw      int    `json:"cloud_monthly_quota_usage_for_this_gw"`
        Cloud_hourly_quota_usage_for_this_gw       int    `json:"cloud_hourly_quota_usage_for_this_gw"`
        Cloud_monthly_quota_usage_for_quota_id     int    `json:"cloud_monthly_quota_usage_for_quota_id"`
        Cloud_hourly_quota_usage_for_quota_id      int    `json:"cloud_hourly_quota_usage_for_quota_id"`
        Monthly_exceeded_quota                     int    `json:"monthly_exceeded_quota"`
        Hourly_exceeded_quota                      int    `json:"hourly_exceeded_quota"`
        Cloud_quota_max_allow_to_exceed_percentage int    `json:"cloud_quota_max_allow_to_exceed_percentage"`
        Pod_time_gmt                               string `json:"pod_time_gmt"`
        Quota_expiration                           string `json:"quota_expiration"`
        Action                                     string `json:"action"`
    } `json:"response"`
}

func main() {

    const jsonPayload = `{
    "response": [{
        "remain_quota_hour": 500,
        "remain_quota_month": 10000,
        "assigned_quota_hour": 500,
        "assigned_quota_month": 10000,
        "hourly_quota_next_reset": "1508464800",
        "monthly_quota_next_reset": "1509494400",
        "quota_id": "H973AA8",
        "cloud_monthly_quota_period_start": "1506816000",
        "cloud_monthly_quota_usage_for_this_gw": 0,
        "cloud_hourly_quota_usage_for_this_gw": 0,
        "cloud_monthly_quota_usage_for_quota_id": 0,
        "cloud_hourly_quota_usage_for_quota_id": 0,
        "monthly_exceeded_quota": 0,
        "hourly_exceeded_quota": 0,
        "cloud_quota_max_allow_to_exceed_percentage": 1000,
        "pod_time_gmt": "1508461217",
        "quota_expiration": "1510358400",
        "action": "ALLOW"
    }]
}`
    var data Response
    json.NewDecoder(strings.NewReader(jsonPayload)).Decode(&data)

    fmt.Printf("%+v\n", data)

}

控制台输出:

=> {Quotas:[{Remain_quota_hour:500 Remain_quota_month:10000 Assigned_quota_hour:500 Assigned_quota_month:10000 Hourly_quota_next_reset:1508464800 Monthly_quota_next_reset:1509494400 Quota_id:H973AA8 Cloud_monthly_quota_period_start:1506816000 Cloud_monthly_quota_usage_for_this_gw:0 Cloud_hourly_quota_usage_for_this_gw:0 Cloud_monthly_quota_usage_for_quota_id:0 Cloud_hourly_quota_usage_for_quota_id:0 Monthly_exceeded_quota:0 Hourly_exceeded_quota:0 Cloud_quota_max_allow_to_exceed_percentage:1000 Pod_time_gmt:1508461217 Quota_expiration:1510358400 Action:ALLOW}]}

Go Play

<子> 这里有一个提示,将json有效负载转换为struct http://json2struct.mervine.net/

答案 1 :(得分:0)

除了将类型定义为@Зелёный之外,您还可以使用匿名结构。当您只需要一次结构时,这非常有用:

var response struct {
    Quotas []Quota `json:"response"`
}

代码:

package main

import (
    "encoding/json"
    "fmt"
    "strings"
)

type Quota struct {
    Remain_quota_hour                          int    `json:"remain_quota_hour"`
    Remain_quota_month                         int    `json:"remain_quota_month"`
    Assigned_quota_hour                        int    `json:"assigned_quota_hour"`
    Assigned_quota_month                       int    `json:"assigned_quota_month"`
    Hourly_quota_next_reset                    string `json:"hourly_quota_next_reset"`
    Monthly_quota_next_reset                   string `json:"monthly_quota_next_reset"`
    Quota_id                                   string `json:"quota_id"`
    Cloud_monthly_quota_period_start           string `json:"cloud_monthly_quota_period_start"`
    Cloud_monthly_quota_usage_for_this_gw      int    `json:"cloud_monthly_quota_usage_for_this_gw"`
    Cloud_hourly_quota_usage_for_this_gw       int    `json:"cloud_hourly_quota_usage_for_this_gw"`
    Cloud_monthly_quota_usage_for_quota_id     int    `json:"cloud_monthly_quota_usage_for_quota_id"`
    Cloud_hourly_quota_usage_for_quota_id      int    `json:"cloud_hourly_quota_usage_for_quota_id"`
    Monthly_exceeded_quota                     int    `json:"monthly_exceeded_quota"`
    Hourly_exceeded_quota                      int    `json:"hourly_exceeded_quota"`
    Cloud_quota_max_allow_to_exceed_percentage int    `json:"cloud_quota_max_allow_to_exceed_percentage"`
    Pod_time_gmt                               string `json:"pod_time_gmt"`
    Quota_expiration                           string `json:"quota_expiration"`
    Action                                     string `json:"action"`
}

func main() {

    const payload = `{
    "response": [{
        "remain_quota_hour": 500,
        "remain_quota_month": 10000,
        "assigned_quota_hour": 500,
        "assigned_quota_month": 10000,
        "hourly_quota_next_reset": "1508464800",
        "monthly_quota_next_reset": "1509494400",
        "quota_id": "H973AA8",
        "cloud_monthly_quota_period_start": "1506816000",
        "cloud_monthly_quota_usage_for_this_gw": 0,
        "cloud_hourly_quota_usage_for_this_gw": 0,
        "cloud_monthly_quota_usage_for_quota_id": 0,
        "cloud_hourly_quota_usage_for_quota_id": 0,
        "monthly_exceeded_quota": 0,
        "hourly_exceeded_quota": 0,
        "cloud_quota_max_allow_to_exceed_percentage": 1000,
        "pod_time_gmt": "1508461217",
        "quota_expiration": "1510358400",
        "action": "ALLOW"
    }]
}`
    var response struct {
        Quotas []Quota `json:"response"`
    }

    json.NewDecoder(strings.NewReader(payload)).Decode(&response)

    fmt.Printf("%+v\n", response)
}

您可以在此处试用:https://play.golang.org/p/r2tzDdGlIV