关于json:Golang解析成struct | 珊瑚贝

Golang parsing into struct


我无法将此 json 解析到我的结构中。谁能帮忙解决一下这个

1
{“error”:false,”response”:{“results”:[{“id”:68876,”name”:”cee lo green – big girls”},{“id”:68954,”name”:”charles, ray – the girl friend”},{“id”:69603,”name”:”charlie puth – la girls”},{“id”:68001,”name”:”city girls – careless”},{“id”:68000,”name”:”city girls – millionaire dick”},{“id”:68002,”name”:”city girls – period (we live)”},{“id”:68004,”name”:”city girls – rap shit”},{“id”:68003,”name”:”city girls – runnin”},{“id”:68019,”name”:”clairo – pretty girl”},{“id”:68223,”name”:”cohn, marc – girl of mysterious sorrow”},{“id”:68343,”name”:”contours, the – searching for a girl”}

下面是我的结构
包主

导入 (
“编码/json”
“fmt”
“io/ioutil”
“日志”
“net/http”)

类型测试结构{
SngID 字符串 json:”id”
SngNm 字符串 json:”name”
}
类型内部结构{

1
Result[10] test `json:”results”`

}

类型外部结构{
错误布尔 json:”error”
响应 [] 内部 json:”results”

}

  • 我试图将我的数据结构模仿为 json 的结构。但是 json 数据没有解析到我的结构中。下面是我的代码
  • 我将我的代码添加到我的问题中。你能建议我在这里犯什么错误吗?


您的 JSON 格式错误(检查 json.Unmarshal 返回的错误)。

无论如何,这个结构应该适合你。

1
2
3
4
5
6
7
8
9
    type Response struct {
        Error    bool `json:”error”`
        Response struct {
            Results []struct {
                ID   int    `json:”id”`
                Name string `json:”name”`
            } `json:”results”`
        } `json:”response”`
    }
  • 感谢您的答复。我试过你给的结构。它仍然没有解析 id 和 name 的值 我的输出如下所示: Just parsing bool value for error [{Error:false Response:{Results:[{ID:0 Name:} {ID:0 Name:} {ID:0名称:} {ID:0 名称:} {ID:0 名称:}]}}] 进程以退出代码 0 结束
  • 我用你提供的 json 对其进行了测试,它可以工作:play.golang.org/p/-ccZbeEDtH-。您能否提供错误的游乐场再现?
  • 是的,我也从那里得到了 json 并对其进行了测试。工作正常。
  • 我可以在我的结构中使用映射来解析这个 json 以获取 id 和 name。我尝试使用地图,但没有奏效。


来源:https://www.codenong.com/64072835/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_9442.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?