2014-10-30 110 views
1

我有一個JSON字符串,我從BaseCamp API。我知道JSON是有效的事實,但是,我無法使用Newtonsoft.Json來反序列化對象。JSON C#DeserializeObject錯誤與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; } 
     } 
    }  

我假設我的課程設置有問題,但我看不到它。

+2

你如何試圖反序列化JSON?這似乎是多個RootObjects的列表,因此您必須確保將其反序列化爲列表或數組,而不是一個RootObject。 – 2014-10-30 19:11:21

+0

var json = JsonConvert.DeserializeObject (response);這對我有效。事實上,我剛剛在應用程序中使用它。響應是我從api獲得的json字符串。 – pormus 2014-10-30 19:13:55

+1

這適用於json字符串只包含1個RootObject的情況,但在給定的情況下它包含兩個。因此,您需要將其反序列化爲RootObjects集合 – 2014-10-30 19:18:58

回答

2

你需要轉換到RootObject數組:

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

或列表(或者你想爲此事的任何其他集合)...

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

做完之後,我該如何調用不同的值?之前我只會做json.name或json.id.這似乎不再適用於此。 – pormus 2014-10-30 19:41:14

+0

您可以使用'foreach(json中的var item)'逐個訪問所有項目,或者'json [0]'訪問第一個項目,'json [1]'訪問第二個項目等等...... – Yogesh 2014-10-30 19:43:25

+0

那麼公共類RootObject {public int expires_in {get;組; } public string access_token {get;組; } public string refresh_token {get;組; } }和我在那裏的那個? 我能夠使用我的舊方法。 – pormus 2014-10-30 19:48:31