dsizd368332 2018-08-18 00:34
浏览 56
已采纳

动态JSON结构,API结果golang

I have to make two HTTP API calls in GoLang, the first API call returns this json response:

{
  "status": 200,
  "msg": "OK",
  "result": {
    "id": "24",
    "folderid": "4248"
  }
}

My json struct for first response is setup like this:

type One struct {
    Status int    `json:"status"`
    Msg    string `json:"msg"`
    Result struct {
        ID       string `json:"id"`
        Folderid string `json:"folderid"`
    } `json:"result"`
}

The second call is where the problem is. As you can see the first API call returns a result -> id. This ID should be the name for the beginning of my second struct, but I can't seem how to make it dynamic or put a result as my structure name. This ID (24) will always change based on the first API call. I have no way currently to parse the second call's JSON and setup my struct. On the second API call I want to access the remoteurl/status.

Second call result (I can not parse):

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

Does anyone know how to setup my struct or go about this. I am a new programmer and go and have worked on this for 4 days. And decided to ask for some help, since I am in school and have normal homework.

  • 写回答

1条回答 默认 最新

  • douxing2652 2018-08-18 03:00
    关注
    {
      "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
        }
      }
    }
    

    Is not value JSON. You MUST mean the JSON i posted below, if you want to check yourself, copy your version of the JSON into any JSON validator;

    https://jsonlint.com/

    https://jsoneditoronline.org/

    https://jsonformatter.curiousconcept.com/

    Also view the thread linked below.. if the API truly is returning what you claim it returns then the API has a bug in it

    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
        }
      }
    }
    

    Here is some example code that uses a map to a struct what solves the dynamic response of the second response

    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 
    
    ", b)
        b, _ = json.MarshalIndent(two, "", " ")
        fmt.Printf("%s 
    
    ", 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
    %s
    ", b)
        }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,