动态JSON结构,API结果golang

动态JSON结构,API结果golang,json,api,http,parsing,go,Json,Api,Http,Parsing,Go,我必须在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

我必须在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调用返回一个result->id。这个id应该是我的第二个结构的开头的名称,但是我似乎不知道如何使它成为动态的或将结果作为我的结构名称。此ID(24)将始终根据第一次API调用进行更改。我目前无法解析第二个调用的JSON并设置我的结构。在第二个API调用中,我希望访问remoteurl/状态

第二次调用结果(我无法解析):

是否有人知道如何设置我的结构或进行此操作。我是一个新的程序员,并且已经为此工作了4天。并决定寻求一些帮助,因为我在学校,有正常的家庭作业

发现使用有助于解决未来的问题,将基于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验证器中

还可以查看下面链接的线程。。如果API确实返回了您声称它返回的内容,那么API中就有一个bug

下面是一些示例代码,它使用结构的映射来解决第二个响应的动态响应

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

}

第二个响应json无效,因为24不是字符串。这真的是响应吗?是的,它来自OpenLoadAPI,您可以阅读他们的文档。这就是确切的回答。@Raymond不管openload文档怎么说,问题中的第二个回答都不是有效的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
    }
  }
}
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)
    }

}