fongWH 2017-04-15 04:13 采纳率: 0%
浏览 4224

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-15 04:17
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建