gpt4 book ai didi

json - 使用 Go 发布解析 JSON 文件

转载 作者:数据小太阳 更新时间:2023-10-29 03:44:45 24 4
gpt4 key购买 nike

我发出一个 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 对象,但它需要被解码。

最佳答案

我使用以下方法修复了它: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"`
}

感谢您的意见

非常节省时间!

关于json - 使用 Go 发布解析 JSON 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57291498/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com