![](/img/trans.png)
[英]Mapping JSON returned by REST API containing dynamic keys to a struct in Golang
[英]Dynamic JSON struct, API result 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)
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.