第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

動態(tài) JSON 結(jié)構,API 結(jié)果 golang

動態(tài) JSON 結(jié)構,API 結(jié)果 golang

Go
倚天杖 2023-03-21 15:36:00
我必須在 GoLang 中進行兩次 HTTP API 調(diào)用,第一個 API 調(diào)用返回此 json 響應:{  "status": 200,  "msg": "OK",  "result": {    "id": "24",    "folderid": "4248"  }}我的第一個響應的 json 結(jié)構是這樣設置的:type One struct {    Status int    `json:"status"`    Msg    string `json:"msg"`    Result struct {        ID       string `json:"id"`        Folderid string `json:"folderid"`    } `json:"result"`}第二個電話就是問題所在。如您所見,第一個 API 調(diào)用返回一個結(jié)果 -> id。此 ID 應該是我的第二個結(jié)構的開頭名稱,但我似乎無法使其動態(tài)化或?qū)⒔Y(jié)果作為我的結(jié)構名稱。此 ID (24) 將始終根據(jù)第一個 API 調(diào)用而更改。我目前無法解析第二個調(diào)用的 JSON 并設置我的結(jié)構。在第二次 API 調(diào)用中,我想訪問 remoteurl/status。第二次調(diào)用結(jié)果(我無法解析):{  "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    }  }}有誰知道如何設置我的結(jié)構或如何解決這個問題。我是一名新程序員,已經(jīng)為此工作了 4 天。并決定尋求幫助,因為我在學校并且有正常的家庭作業(yè)。發(fā)現(xiàn)使用JSON-to-GO有助于解決未來的問題,將基于 JSON 內(nèi)容創(chuàng)建結(jié)構和其他必需品。
查看完整描述

1 回答

?
ibeautiful

TA貢獻1993條經(jīng)驗 獲得超6個贊

{

  "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。你必須指的是我在下面發(fā)布的 JSON,如果你想檢查自己,請將你的 JSON 版本復制到任何 JSON 驗證器中;


https://jsonlint.com/


https://jsoneditoronline.org/


https://jsonformatter.curiousconcept.com/


另請查看下面鏈接的線程。如果 API 確實返回了您聲稱返回的內(nèi)容,則該 API 中存在錯誤


為什么 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

    }

  }

}

下面是一些使用映射到結(jié)構的示例代碼,它解決了第二個響應的動態(tài)響應


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)

    }


}



查看完整回答
反對 回復 2023-03-21
  • 1 回答
  • 0 關注
  • 105 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號