Newtonsoft.JSON的JSON C#反序列化对象错误

本文关键字:JSON 对象 错误 反序列化 Newtonsoft | 更新日期: 2023-09-27 18:28:33

我有一个JSON字符串,它是从BaseCamp API获得的。我知道JSON是有效的,但是,我不能使用Newtonsoft.JSON.来反序列化对象

我得到一个错误说:

Cannot deserialize the current JSON array (e.g.[1,2,3]) into type BaseCamp.Code.Projects+RootObject because the type requires a JSON objet (e.g. {"name":"value"}) to deserialize correctly.

JSON(从API返回的未格式化内容减去URL的值)

[
    {
        "id":6656986,
        "name":"Physics Revamp",
        "description":"ISU department of physics website redesign",
        "archived":false,
        "is_client_project":true,
        "created_at":"2014-08-07T10:59:29.000-05:00",
        "updated_at":"2014-10-30T09:18:01.000-05:00",
        "trashed":false,
        "color":"2c5322",
        "draft":false,
        "template":false,
        "last_event_at":"2014-10-30T09:18:01.000-05:00",
        "starred":false,
        "url":"xxxxxxxxxxxxxxxxxxxxxxx",
        "app_url":"xxxxxxxxxxxxxxx"
    },
    {
        "id":7178664,
        "name":"Physics Videos",
        "description":"",
        "archived":false,
        "is_client_project":false,
        "created_at":"2014-10-02T08:34:46.000-05:00",
        "updated_at":"2014-10-23T08:40:17.000-05:00",
        "trashed":false,
        "color":"660099",
        "draft":false,
        "template":false,
        "last_event_at":"2014-10-23T08:40:17.000-05:00",
        "starred":false,
        "url":"xxxxxxxxxxxxxxxxxxxxxxx",
        "app_url":"xxxxxxxxxxxxxxxxxxx"
    },
    {
        "id":6685451,
        "name":"WZND Website 2014",
        "description":"",
        "archived":false,
        "is_client_project":true,
        "created_at":"2014-08-11T13:25:51.000-05:00",
        "updated_at":"2014-10-30T11:26:39.000-05:00",
        "trashed":false,
        "color":"3185c5",
        "draft":false,
        "template":false,
        "last_event_at":"2014-10-30T11:26:39.000-05:00",
        "starred":false,
        "url":"xxxxxxxxxxxxxxxxxx",
        "app_url":"xxxxxxxxxxxxxxxxx"
    }
]

我的C#类:

public class Projects  
    {  
        public class RootObject  
        {  
            public int id { get; set; }  
            public string name { get; set; }  
            public string description { get; set; }  
            public bool archived { get; set; }  
            public bool is_client_project { get; set; }  
            public string created_at { get; set; }  
            public string updated_at { get; set; }  
            public bool trashed { get; set; }  
            public string color { get; set; }  
            public bool draft { get; set; }  
            public bool template { get; set; }  
            public string last_event_at { get; set; }  
            public bool starred { get; set; }  
            public string url { get; set; }  
            public string app_url { get; set; }  
        }  
    }    

我假设我的类的设置方式有问题,但我看不出来。

Newtonsoft.JSON的JSON C#反序列化对象错误

您需要转换为RootObject:的数组

var json = JsonConvert.DeserializeObject<Projects.RootObject[]>(response); 

或列表(或您想要的任何其他集合)。。。

var json = JsonConvert.DeserializeObject<List<Projects.RootObject>>(response);