【问题标题】:Issue parsing JSON file with Go使用 Go 解析 JSON 文件的问题
【发布时间】:2019-07-31 13:07:51
【问题描述】:

我发出一个 GET 请求,并收到一个我无法解析的 JSON 文件。

这是我必须解析的数据

{
    "codeConv": "ACC00000321",
    "start": "2019-07-01T00:00:00Z",
    "end": "2019-08-21T00:00:00Z",
    "details": [
        {
            "idPrm": "30000000123456",
            "keys": [
                {
                    "timestamp": "2019-07-01T00:00:00Z",
                    "value": 0
                },
                {
                    "timestamp": "2019-07-01T00:30:00Z",
                    "value": 0
                },
                ...
            ]
        }, 
        {
            "idPrm": "30000000123457",
            "keys": [
                {
                    "timestamp": "2019-07-01T00:00:00Z",
                    "value": 1
                },
                {
                    "timestamp": "2019-07-01T00:30:00Z",
                    "value": 2
                },
                ...
            ]
        }
    ]
}

这是我的对象:

type APIMain struct {
    CodeConv string          `json:"codeConv"`
    Start    string          `json:"start"`
    End      []Keys          `json:"end"`
    Details  []APIData `json:"details"`
}

//APIData match the data we receive from api
type APIData struct {
    Prm  string `json:"idPrm"`
    Keys []Keys `json:"keys"`
}

type Keys struct {
    Timestamp string `json:"timestamp"`
    Value     string `json:"value"`
}

这是通过基本身份验证获取数据的方法:

tr := &http.Transport{
        TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
    }
    client := &http.Client{Transport: tr}
    req, err := http.NewRequest("GET", url, nil)

    if err != nil {
        return nil, err
    }
    if login != "" && password != "" {
        req.SetBasicAuth(login, password)
    }

    response, err := client.Do(req)
    //defer response.Body.Close()
    if err != nil {
        return nil, err
    }
    if response.StatusCode != 200 {
        fmt.Println(response.Body)
        panic(response.Status)
    }

    err = json.NewDecoder(response.Body).Decode(&result)
    fmt.Println("result", result) // result is empty array

如何查看问题是在请求中,还是在解析中?

我有一个response.Body 对象,但需要对其进行解码。

【问题讨论】:

  • 响应 JSON 不是一个数组而是一个对象,所以如果您传递 result,根据您的评论它是一个数组("result is empty array" ) 那么这就是你的问题。
  • ...如果您显示result变量的声明会更容易帮助您。
  • 你不应该使用或打印result而不检查错误返回err

标签: json go get


【解决方案1】:

我修复了它:https://mholt.github.io/json-to-go/

它生成了这个结构:

type AutoGenerated struct {
    CodeConv string    `json:"codeConv"`
    Start    time.Time `json:"start"`
    End      time.Time `json:"end"`
    Details  []struct {
        IDPrm string `json:"idPrm"`
        Keys  []struct {
            Timestamp time.Time `json:"timestamp"`
            Value     float64   `json:"value"`
        } `json:"keys"`
    } `json:"details"`
}

感谢您的cmets

非常节省时间!

【讨论】:

    猜你喜欢
    • 2021-09-18
    • 2015-08-29
    • 1970-01-01
    • 2016-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-26
    相关资源
    最近更新 更多