json - 动态 JSON 结构,API 结果 golang
问题描述
我必须在 GoLang 中进行两个 HTTP API 调用,第一个 API 调用返回这个 json 响应:
{
"status": 200,
"msg": "OK",
"result": {
"id": "24",
"folderid": "4248"
}
}
我的第一个响应的 json 结构是这样设置的:
type One struct {
Status int `json:"status"`
Msg string `json:"msg"`
Result struct {
ID string `json:"id"`
Folderid string `json:"folderid"`
} `json:"result"`
}
第二个电话是问题所在。如您所见,第一个 API 调用返回一个结果 -> id。此 ID 应该是我的第二个结构的开头的名称,但我似乎无法使其动态或将结果作为我的结构名称。此 ID (24) 将始终根据第一个 API 调用而更改。我目前无法解析第二个调用的 JSON 并设置我的结构。在第二个 API 调用中,我想访问 remoteurl/status。
第二次调用结果(我无法解析):
{
"status": 200,
"msg": "OK",
"result": {
24: ** THIS IS DYNAMIC** {
"id": 24,
"remoteurl": "http://proof.ovh.net/files/100Mio.dat",
"status": "new",
"bytes_loaded": null,
"bytes_total": null,
"folderid": "4248",
"added": "2015-02-21 09:20:26",
"last_update": "2015-02-21 09:20:26",
"extid": false,
"url": false
}
}
}
有谁知道如何设置我的结构或解决这个问题。我是一名新程序员,并且已经为此工作了 4 天。并决定寻求一些帮助,因为我在学校并且有正常的作业。
发现使用JSON-to-GO有助于解决未来的问题,将基于 JSON 内容创建结构和其他必需品。
解决方案
{
"status": 200,
"msg": "OK",
"result": {
24: {
"id": 24,
"remoteurl": "http://proof.ovh.net/files/100Mio.dat",
"status": "new",
"bytes_loaded": null,
"bytes_total": null,
"folderid": "4248",
"added": "2015-02-21 09:20:26",
"last_update": "2015-02-21 09:20:26",
"extid": false,
"url": false
}
}
}
不是值 JSON。您必须指的是我在下面发布的 JSON,如果您想检查自己,请将您的 JSON 版本复制到任何 JSON 验证器中;
https://jsonformatter.curiousconcept.com/
另请查看下面链接的线程.. 如果 API 确实返回了您声称返回的内容,则 API 中存在错误
{
"status": 200,
"msg": "OK",
"result": {
"24": {
"id": 24,
"remoteurl": "http://proof.ovh.net/files/100Mio.dat",
"status": "new",
"bytes_loaded": null,
"bytes_total": null,
"folderid": "4248",
"added": "2015-02-21 09:20:26",
"last_update": "2015-02-21 09:20:26",
"extid": false,
"url": false
}
}
}
这是一些示例代码,它使用映射到结构来解决第二个响应的动态响应
package main
import (
"encoding/json"
"fmt"
"log"
)
var res1 = `{
"status": 200,
"msg": "OK",
"result": {
"id": "24",
"folderid": "4248"
}
}`
var res2 = `{
"status": 200,
"msg": "OK",
"result": {
"24": {
"id": 24,
"remoteurl": "http://proof.ovh.net/files/100Mio.dat",
"status": "new",
"bytes_loaded": null,
"bytes_total": null,
"folderid": "4248",
"added": "2015-02-21 09:20:26",
"last_update": "2015-02-21 09:20:26",
"extid": false,
"url": false
}
}
}
`
type One struct {
Status int `json:"status"`
Msg string `json:"msg"`
Result struct {
ID string `json:"id"`
Folderid string `json:"folderid"`
} `json:"result"`
}
type Two struct {
Status int `json:"status"`
Msg string `json:"msg"`
Result map[string]innerData `json:"result"`
}
type innerData struct {
ID int `json:"id"`
Remoteurl string `json:"remoteurl"`
Status string `json:"status"`
BytesLoaded interface{} `json:"bytes_loaded"`
BytesTotal interface{} `json:"bytes_total"`
Folderid string `json:"folderid"`
Added string `json:"added"`
LastUpdate string `json:"last_update"`
Extid bool `json:"extid"`
URL bool `json:"url"`
}
func main() {
var one One
err := json.Unmarshal([]byte(res1), &one)
if err != nil {
log.Fatal(err)
}
var two Two
err = json.Unmarshal([]byte(res2), &two)
if err != nil {
log.Fatal(err)
}
//pretty print both strutures
b, _ := json.MarshalIndent(one, "", " ")
fmt.Printf("%s \n\n", b)
b, _ = json.MarshalIndent(two, "", " ")
fmt.Printf("%s \n\n", b)
// access data from two with id from one
if dat, ok := two.Result[one.Result.ID]; ok {
b, _ = json.MarshalIndent(dat, "", " ")
fmt.Printf("inner data\n%s\n", b)
}
}
推荐阅读
- html - Spring Security JSP 标签属性操作
- next.js - 使用 Next JS + Stripe 解决我未处理的运行时错误
- python - 心率测量和特征通知的转换
- java - springframework:启用 SSL 后启动 ApplicationContext 时出错
- python - 使用 pandas 将多行不同的字符串合并为一个
- javascript - WebAssembly:线程间共享数据的最佳内存策略
- mysql - 如何统计每天的人数并使用 SQL 对他们进行分组?
- python - 将参数传递给 docker-compose 命令
- azure-site-recovery - Site Recovery - Powershell 通过 Azure 自动化“重新保护” - 不工作(Azure 到 Azure)
- javascript - ERESOLVE 无法解析安装 npm 包 @react-native-community/cameraroll 的依赖关系树