fongWH 2017-04-14 20:13 采纳率: 0%
浏览 4249

C# winform JSON反序列化对象报错

反序列化异常:
图片说明
“Newtonsoft.Json.JsonSerializationException”类型的未经处理的异常在 Newtonsoft.Json.dll 中发生

其他信息: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[FolkPokerModes.Role]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.

Path 'player', line 1, position 10.

序列化代码:
JsonSerializer js = new JsonSerializer();

        string str = JsonConvert.SerializeObject(o);

        byte[] buf = Encoding.UTF8.GetBytes(str);
        //发送数据
        //byte[] buf = System.Text.Encoding.Default.GetBytes ( a );
        client.Send(buf);

传递的对象:
public class Role
{
public Role(Player player)
{
this.player = player;
}

    public Role() { }

    public Player player;//玩家信息(玩家与角色绑定)

    public Player Player
    {
        get { return player; }
        set { player = value; }
    }

    public int Location;//角色在界面的哪个位置

    public int location
    {
        get { return Location; }
        set { Location = value; }
    }



    public bool Landlord = false;//角色类型,是否为地主(false为农民,true为地主)

    public bool landlord
    {
        get { return Landlord; }
        set { Landlord = value; }
    }

    public ArrayList Remain_Subscript = new ArrayList();//保存每个角色剩余牌的图形的下标

    public ArrayList Remain_Subscript1
    {
        get { return Remain_Subscript; }
        set { Remain_Subscript = value; }
    }



    public ArrayList Remain_Poker = new ArrayList();//剩余的牌

    public ArrayList remain_poker
    {
        get { return Remain_Poker; }
        set { Remain_Poker = value; }
    }

    public ArrayList Hand_Poker = new ArrayList();//保存上手牌

    public ArrayList hand_poker
    {
        get { return Hand_Poker; }
        set { Hand_Poker = value; }
    }

    public ArrayList Already_Poker = new ArrayList();//已出的所有牌

    public ArrayList already_poker
    {
        get { return Already_Poker; }
        set { Already_Poker = value; }
    }

怎么解决

展开全部

  • 写回答

1条回答 默认 最新

  • threenewbee 2017-04-14 20:17
    关注

    你的类和json不匹配,试图将单个对象反序列化成一个集合

    评论
    编辑
    预览

    报告相同问题?

    手机看
    程序员都在用的中文IT技术交流社区

    程序员都在用的中文IT技术交流社区

    专业的中文 IT 技术社区,与千万技术人共成长

    专业的中文 IT 技术社区,与千万技术人共成长

    关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

    关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

    客服 返回
    顶部