使用Go解析JSON文件时出现问题

使用Go解析JSON文件时出现问题,json,go,get,Json,Go,Get,我发出一个GET请求,并收到一个无法解析的JSON文件 这是我必须解析的数据 { "codeConv": "ACC00000321", "start": "2019-07-01T00:00:00Z", "end": "2019-08-21T00:00:00Z", "details": [ { "idPrm": "30000000123456", "keys": [ {

我发出一个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"`
}
下面是使用basic auth获取数据的方法:

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
如何查看问题是在请求中还是在解析中

我得到了一个
响应。Body
对象,但它需要解码。

我使用以下方法修复了它:

产生这种结构的原因:

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"`
}
谢谢你的评论


非常节省时间

响应JSON不是一个数组,而是一个对象,因此如果您传递的是
result
,根据您的注释它是一个数组(“result是空数组”),那么这就是您的问题。。。。如果显示
result
变量的声明,则会更容易帮助您。如果未检查错误返回
err
,则不应使用或打印
result