doushang3352 2017-10-21 13:03
浏览 45

Unmarshall json响应结构为空

I am trying to unmarshall JSON response and it's coming up empty. I'm sure it's something incredibly stupid that I am missing here!

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 Request and unmarshall:

{
    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
}

I can see via string(body) the JSON is coming down, but nothing assigned to the struct. I was desperate at one point and moved to json.decoder to the same result. Any thoughts?

  • 写回答

2条回答 默认 最新

  • douzhuangxuan3268 2017-10-21 13:15
    关注

    You have a wrong Quotas struct definition, as you can see from the json payload is an array.

    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
    ", data)
    
    }
    

    Console output:

    => {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

    Here is a tip for you, convert json payload to struct here http://json2struct.mervine.net/

    评论

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊