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条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog