doujie1908 2016-07-31 04:42
浏览 147
已采纳

Golang-无法将数字解组为字符串类型的Go值

When trying to json.Unmarshal some JSON code from a website into a struct I created, I receive the following error:

cannot unmarshal number into Go value of type string

Here is my code: https://play.golang.org/p/-5nphV9vPw

  • 写回答

2条回答 默认 最新

  • douka19950505 2016-07-31 05:20
    关注

    this works for me (corrected version):

    package main
    
    import (
        "encoding/json"
        "fmt"
        "log"
    )
    
    type movie struct {
        Adult         bool
        Backdrop_path string
        Budget        int
        Genres        []struct {
            Id   int // string
            Name string
        }
        Homepage             string
        Id                   int
        Imdb_id              string
        Original_language    string
        Original_title       string
        Overview             string
        Popularity           float64 //    string
        Poster_path          string
        Production_companies []struct {
            Name string
            Id   int
        }
        Production_countries []struct {
            Name string
        }
        Release_date     string
        Revenue          int
        Runtime          int
        Spoken_languages []struct {
            Name string
        }
        Status       string
        Tagline      string
        Title        string
        Video        bool
        Vote_average float64
        Vote_count   int
        Embedurl     string
    }
    
    func main() {
        var movieData movie
        str := `
        {
        "adult":false,
        "backdrop_path":"/mWuHbFc7qVmVcpybx3ezhXLj5VO.jpg",
        "belongs_to_collection":null,
        "budget":25000000,
        "genres":
            [
            {
                "id":35,
                "name":"Comedy"
            },
            {
                "id":37,
                "name":"Western"
            }
            ],
        "homepage":"",
        "id":8388,
        "imdb_id":"tt0092086",
        "original_language":"en",
        "original_title":"¡Three Amigos!",
        "overview":"Three unemployed actors accept an invitation to a Mexican village to replay their bandit fighter roles, unaware that it is the real thing.",
        "popularity":0.799492,
        "poster_path":"/ehCzedovkiM8CnDeuSSHlRbdfxI.jpg",
        "production_companies":
        [{
            "name":"L.A. Films",
            "id":960
         },
         {
            "name":"Home Box Office (HBO)",
            "id":3268
         }],
        "production_countries":
        [{
            "iso_3166_1":"US",
            "name":"United States of America"
        }],
        "release_date":"1986-12-12",
        "revenue":0,
        "runtime":102,
        "spoken_languages":
        [{
            "iso_639_1":"en",
            "name":"English"
        },{
            "iso_639_1":"de",
            "name":"Deutsch"
        },{
            "iso_639_1":"es",
            "name":"Español"
        }],
        "status":"Released",
        "tagline":"They're Down On Their Luck And Up To Their Necks In Senoritas, Margaritas, Banditos And Bullets!",
        "title":"Three Amigos",
        "video":false,
        "vote_average":6.2,
        "vote_count":116
        }`
        err := json.Unmarshal([]byte(str), &movieData)
        if err != nil {
            log.Fatal(err)
        }
        fmt.Println(movieData)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题