ga3ga3 2016-12-23 02:30 采纳率: 50%
浏览 1783
已采纳

json 反序列化问题。 请各位大神看看 c#

json 问题
图片说明

程序代码如下:
res=@"{"marks":{"marks":479998,"visitNumber":0,"bikePasswords":[{"password":"4106","affirmNumber":0,"errorNumber":0,"id":113,"createUser":"admin","createDate":"Dec 6, 2016 3:04:06 PM"}],"id":113,"createUser":"admin","createDate":"Dec 6, 2016 3:04:06 PM"},"resultType":"success"}"

程序调用:
List<SPTA.bbb> o = JsonConvert.DeserializeObject<List<SPTA.bbb>>(res);

运行代码报错。如下。
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[SPTA.bbb]' 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 'marks', line 1, position 9.

不知道是实体类有问题还是撒的。各位大神看看

通过生成实体类为

public class BikePasswords
{
    /// <summary>
    /// 
    /// </summary>
    public string password { get; set; }
    /// <summary>
    /// AffirmNumber
    /// </summary>
    public int affirmNumber { get; set; }
    /// <summary>
    /// ErrorNumber
    /// </summary>
    public int errorNumber { get; set; }
    /// <summary>
    /// Id
    /// </summary>
    public int id { get; set; }
    /// <summary>
    /// admin
    /// </summary>
    public string createUser { get; set; }
    /// <summary>
    /// Dec 6, 2016 3:04:06 PM
    /// </summary>
    public string createDate { get; set; }
}

public class Marks
{
    /// <summary>
    /// Marks
    /// </summary>
    public int marks { get; set; }
    /// <summary>
    /// VisitNumber
    /// </summary>
    public int visitNumber { get; set; }
    /// <summary>
    /// BikePasswords
    /// </summary>
    public List<BikePasswords> bikePasswords { get; set; }
    /// <summary>
    /// Id
    /// </summary>
    public int id { get; set; }
    /// <summary>
    /// admin
    /// </summary>
    public string createUser { get; set; }
    /// <summary>
    /// Dec 6, 2016 3:04:06 PM
    /// </summary>
    public string createDate { get; set; }
}

public class bbb
{
    /// <summary>
    /// Marks
    /// </summary>
    public Marks marks { get; set; }
    /// <summary>
    /// success
    /// </summary>
    public string resultType { get; set; }
  • 写回答

4条回答 默认 最新

  • Go 旅城通票 2016-12-23 03:06
    关注

    你的res是Marks类,不是Marks数组啊。。

     List<SPTA.bbb> o = JsonConvert.DeserializeObject<List<SPTA.bbb>>(res);
    ==》
    Marks o = JsonConvert.DeserializeObject<Marks>(res);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 请问这种量表怎么用spss量化分析(作为中介模型的因变量
  • ¥55 AD844 howland电流源如何驱动大额负载
  • ¥15 C++ /QT 内存权限的判断函数列举
  • ¥15 深度学习GFnet理解问题
  • ¥15 单细胞小提琴堆叠图代码
  • ¥50 升级strust2版本到2.3.15.1后使用ognl3.0.6.jar windows环境中没有问题,但部署到linux环境报错
  • ¥15 vue页面,node封装接口
  • ¥15 求TMS320F280039C工程模板!
  • ¥15 delphi+fastreport实现分组补空打印问题
  • ¥15 使用python把两台mysql数据库服务器数据导出和导入