代码之家  ›  专栏  ›  技术社区  ›  ABC

动态JSON结构,API结果golang

  •  0
  • ABC  · 技术社区  · 8 年前

    {
      "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"`
    }
    

    第二个调用结果(我无法分析):

    {
      "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天。决定寻求帮助,因为我在学校,有正常的家庭作业。

    1 回复  |  直到 8 年前
        1
  •  1
  •   reticentroot    8 年前
    {
      "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://jsonlint.com/

    https://jsoneditoronline.org/

    https://jsonformatter.curiousconcept.com/

    同时查看下面链接的线程。。如果API真的返回了您声称它返回的内容,那么API中就有一个bug

    Why JSON allows only string to be a key?

    {
      "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)
        }
    
    }