英文:
Unable to unmarshal number into Go Value
问题
我尝试避免问一些简单答案的问题,但我似乎无法弄清楚问题出在哪里...(标题中的问题)
相关代码:
match := new(Match)
if _, msgB, err = ws.ReadMessage(); err != nil {
panic(err)
} else {
println(string(msgB))
err = json.Unmarshal(msgB, match)
if err != nil { panic(err) }
}
type Match struct {
Teams [][]Char
Map [][]Tile
ID string //uuid
Socket *websocket.Conn `json:'-'`
}
type Char struct {
ID int
HP int
CT int
Stats statList
X int
Y int
ACList Actions
}
type statList struct {
Str int
Vit int
Int int
Wis int
Dex int
Spd int
}
type Actions struct {
Actions []Action
TICKCT int
}
要解组的字符串(为了可见性进行格式化):
{
"Teams": [
[
{
"ID": 1,
"HP": 10,
"CT": 0,
"Stats": [
1,
1,
1,
1,
1,
1
],
"X": 0,
"Y": 0,
"ACList": {
"Actions": [],
"TICKCT": 0
}
}
],
[
{
"ID": 2,
"HP": 10,
"CT": 0,
"Stats": [
1,
1,
1,
1,
1,
1
],
"X": 2,
"Y": 2,
"ACList": {
"Actions": [],
"TICKCT": 0
}
}
]
],
"Map": [
[
{
"Depth": 1,
"Type": 1,
"Unit": 1
},
{
"Depth": 1,
"Type": 1,
"Unit": null
},
{
"Depth": 1,
"Type": 1,
"Unit": null
}
],
[
{
"Depth": 1,
"Type": 1,
"Unit": null
},
{
"Depth": 1,
"Type": 1,
"Unit": null
},
{
"Depth": 1,
"Type": 1,
"Unit": null
}
],
[
{
"Depth": 1,
"Type": 1,
"Unit": null
},
{
"Depth": 1,
"Type": 1,
"Unit": null
},
{
"Depth": 1,
"Type": 1,
"Unit": 2
}
]
],
"ID": "0b055e19-9b96-e492-b816-43297f12cc39"
}
错误:
2014/03/28 12:11:41 http: panic serving 127.0.0.1:56436: json: cannot unmarshal number into Go value of type main.Char
英文:
I try to refrain from asking questions with simple answers but I can't seem to figure out what the issue is here... (Issue in title)
Relevant code:
match := new(Match)
if _, msgB, err = ws.ReadMessage(); err != nil {
panic(err)
}else {
println(string(msgB))
err = json.Unmarshal(msgB, match)
if err != nil { panic(err) }
}
type Match struct {
Teams [][]Char
Map [][]Tile
ID string //uuid
Socket *websocket.Conn `json:'-'`
}
type Char struct {
ID int
HP int
CT int
Stats statList
X int
Y int
ACList Actions
}
type statList struct {
Str int
Vit int
Int int
Wis int
Dex int
Spd int
}
type Actions struct {
Actions []Action
TICKCT int
}
String to unmarshal (Formatted for visibility):
{
"Teams": [
[
{
"ID": 1,
"HP": 10,
"CT": 0,
"Stats": [
1,
1,
1,
1,
1,
1
],
"X": 0,
"Y": 0,
"ACList": {
"Actions": [],
"TICKCT": 0
}
}
],
[
{
"ID": 2,
"HP": 10,
"CT": 0,
"Stats": [
1,
1,
1,
1,
1,
1
],
"X": 2,
"Y": 2,
"ACList": {
"Actions": [],
"TICKCT": 0
}
}
]
],
"Map": [
[
{
"Depth": 1,
"Type": 1,
"Unit": 1
},
{
"Depth": 1,
"Type": 1,
"Unit": null
},
{
"Depth": 1,
"Type": 1,
"Unit": null
}
],
[
{
"Depth": 1,
"Type": 1,
"Unit": null
},
{
"Depth": 1,
"Type": 1,
"Unit": null
},
{
"Depth": 1,
"Type": 1,
"Unit": null
}
],
[
{
"Depth": 1,
"Type": 1,
"Unit": null
},
{
"Depth": 1,
"Type": 1,
"Unit": null
},
{
"Depth": 1,
"Type": 1,
"Unit": 2
}
]
],
"ID": "0b055e19-9b96-e492-b816-43297f12cc39"}
Error:
> 2014/03/28 12:11:41 http: panic serving 127.0.0.1:56436: json: cannot
> unmarshal number into Go value of type main.Char
答案1
得分: 1
我修改了代码的固定版本(playground)。这似乎是主要的错误:
type Char struct {
ID int
HP int
CT int
Stats []int // 这里是 statList,这样是行不通的
X int
Y int
ACList Actions
}
另外请注意我对 Tile
的定义,允许数字为 nil
。
type Tile struct {
Depth int
Type int
Unit *int
}
你没有提供所有的结构,所以我自己编写了一些——可能是错误的!总的来说,代码如下:
import (
"encoding/json"
"fmt"
)
type Match struct {
Teams [][]Char
Map [][]Tile
ID string //uuid
// Socket *websocket.Conn `json:'-'`
}
type Char struct {
ID int
HP int
CT int
Stats []int // 这里是 statList,这样是行不通的
X int
Y int
ACList Actions
}
type statList struct {
Str int
Vit int
Int int
Wis int
Dex int
Spd int
}
type Action string
type Actions struct {
Actions []Action
TICKCT int
}
type Tile struct {
Depth int
Type int
Unit *int
}
var data = `{
"Teams": [
[
{
"ID": 1,
"HP": 10,
"CT": 0,
"Stats": [
1,
1,
1,
1,
1,
1
],
"X": 0,
"Y": 0,
"ACList": {
"Actions": [],
"TICKCT": 0
}
}
],
[
{
"ID": 2,
"HP": 10,
"CT": 0,
"Stats": [
1,
1,
1,
1,
1,
1
],
"X": 2,
"Y": 2,
"ACList": {
"Actions": [],
"TICKCT": 0
}
}
]
],
"Map": [
[
{
"Depth": 1,
"Type": 1,
"Unit": 1
},
{
"Depth": 1,
"Type": 1,
"Unit": null
},
{
"Depth": 1,
"Type": 1,
"Unit": null
}
],
[
{
"Depth": 1,
"Type": 1,
"Unit": null
},
{
"Depth": 1,
"Type": 1,
"Unit": null
},
{
"Depth": 1,
"Type": 1,
"Unit": null
}
],
[
{
"Depth": 1,
"Type": 1,
"Unit": null
},
{
"Depth": 1,
"Type": 1,
"Unit": null
},
{
"Depth": 1,
"Type": 1,
"Unit": 2
}
]
],
"ID": "0b055e19-9b96-e492-b816-43297f12cc39"}`
func main() {
match := new(Match)
err := json.Unmarshal([]byte(data), match)
if err != nil {
panic(err)
}
fmt.Printf("match = %#v\n", match)
}
英文:
I made a fixed version of the code (playground). This seemed to be the main mistake:
type Char struct {
ID int
HP int
CT int
Stats []int // This was statList which won't work
X int
Y int
ACList Actions
}
Also note the definition I made of Tile
which allows numbers to be nil
.
type Tile struct {
Depth int
Type int
Unit *int
}
You didn't provide all the structures so I made some up - probably wrong! All together that is:
import (
"encoding/json"
"fmt"
)
type Match struct {
Teams [][]Char
Map [][]Tile
ID string //uuid
// Socket *websocket.Conn `json:'-'`
}
type Char struct {
ID int
HP int
CT int
Stats []int // This was statList which won't work
X int
Y int
ACList Actions
}
type statList struct {
Str int
Vit int
Int int
Wis int
Dex int
Spd int
}
type Action string
type Actions struct {
Actions []Action
TICKCT int
}
type Tile struct {
Depth int
Type int
Unit *int
}
var data = `{
"Teams": [
[
{
"ID": 1,
"HP": 10,
"CT": 0,
"Stats": [
1,
1,
1,
1,
1,
1
],
"X": 0,
"Y": 0,
"ACList": {
"Actions": [],
"TICKCT": 0
}
}
],
[
{
"ID": 2,
"HP": 10,
"CT": 0,
"Stats": [
1,
1,
1,
1,
1,
1
],
"X": 2,
"Y": 2,
"ACList": {
"Actions": [],
"TICKCT": 0
}
}
]
],
"Map": [
[
{
"Depth": 1,
"Type": 1,
"Unit": 1
},
{
"Depth": 1,
"Type": 1,
"Unit": null
},
{
"Depth": 1,
"Type": 1,
"Unit": null
}
],
[
{
"Depth": 1,
"Type": 1,
"Unit": null
},
{
"Depth": 1,
"Type": 1,
"Unit": null
},
{
"Depth": 1,
"Type": 1,
"Unit": null
}
],
[
{
"Depth": 1,
"Type": 1,
"Unit": null
},
{
"Depth": 1,
"Type": 1,
"Unit": null
},
{
"Depth": 1,
"Type": 1,
"Unit": 2
}
]
],
"ID": "0b055e19-9b96-e492-b816-43297f12cc39"}`
func main() {
match := new(Match)
err := json.Unmarshal([]byte(data), match)
if err != nil {
panic(err)
}
fmt.Printf("match = %#v\n", match)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论