ga3ga3
ga3ga3
2016-12-23 02:30
采纳率: 50%
浏览 1.7k

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条回答 默认 最新

  • showbo
    已采纳

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

     List<SPTA.bbb> o = JsonConvert.DeserializeObject<List<SPTA.bbb>>(res);
    ==》
    Marks o = JsonConvert.DeserializeObject<Marks>(res);
    
    点赞 评论
  • ga3ga3
    ga3ga3 2016-12-23 02:36

    100年问一个问题,好久不编程了,只有7分可以送了。呵呵哒

    点赞 评论
  • qq_36809233
    qq_36809233 2016-12-23 03:08

    感觉你应该用集合接收

    点赞 评论
  • ga3ga3
    ga3ga3 2016-12-23 04:24

    这种方法的话
    Marks o = JsonConvert.DeserializeObject(res);

    报错。
    Unexpected character encountered while parsing value: {. Path 'marks', line 1, position 10.

    其实每次就一条记录。
    不过你提醒了我。我改成
    SPTA.bbb o = JsonConvert.DeserializeObject(res);
    就ok了

    点赞 评论

相关推荐