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 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退