Golang - 无法将数字解组为字符串类型的 Go 值
Golang - Cannot unmarshal number into Go value of type string
当我尝试 json.Unmarshal
一些 JSON 网站代码到我创建的结构中时,我收到以下错误:
cannot unmarshal number into Go value of type string
结构定义中存在多个错误。这里是固定版本。
这对我有用(更正版本):
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)
}
基本上,结构有一些问题;
人们可能想使用 JSON-to-Go
有一个特别的问题,我遇到的是这样的:
json: cannot unmarshal number into Go struct field id
of type string
这意味着 Go 无法将 JSON 数字(例如 "id": 35
)编组为 Go 字符串 属性
- 要么
id
变成字符串"id": "35"
;或
- 将
id
类型更改为 int 类型
type movie struct {
Genres []struct {
Id int
Name string
}
}
当我尝试 json.Unmarshal
一些 JSON 网站代码到我创建的结构中时,我收到以下错误:
cannot unmarshal number into Go value of type string
结构定义中存在多个错误。这里是固定版本。
这对我有用(更正版本):
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)
}
基本上,结构有一些问题; 人们可能想使用 JSON-to-Go
有一个特别的问题,我遇到的是这样的:
json: cannot unmarshal number into Go struct field
id
of type string
这意味着 Go 无法将 JSON 数字(例如 "id": 35
)编组为 Go 字符串 属性
- 要么
id
变成字符串"id": "35"
;或 - 将
id
类型更改为 int 类型
type movie struct {
Genres []struct {
Id int
Name string
}
}