duanjiwei1283 2014-03-28 17:22
浏览 24
已采纳

无法将数字解组为Go Value

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条回答 默认 最新

  • duanqie5741 2014-03-28 19:38
    关注

    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
    ", match)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错