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

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序