2014-11-24 135 views
0

我正在使用box api,並試圖解析json對象到一個類。
這是JSON:解析JSON到對象

{ 
    "type":"folder", 
    "id":"0", 
    "sequence_id":null, 
    "etag":null, 
    "name":"All Files", 
    "created_at":null, 
    "modified_at":null, 
    "description":"", 
    "size":9049537, 
    "path_collection": 
    { 
    "total_count":0,"entries":[] 
    }, 
    "created_by": 
    { 
    "type":"user","id":"","name":"","login":"" 
    }, 
    "modified_by": 
    { 
    "type":"user", 
    "id":"111", 
    "name":"a a", 
    "login":"[email protected]" 
    }, 
    "trashed_at":null, 
    "purged_at":null, 
    "content_created_at":null, 
    "content_modified_at":null, 
    "owned_by": 
    { 
    "type":"user", 
    "id":"111", 
    "name":"a a", 
    "login":"[email protected]" 
    },  
    "shared_link":null, 
    "folder_upload_email":null, 
    "parent":null, 
    "item_status":"active", 
    "item_collection": 
    { 
    "total_count":4, 
    "entries": 
    [ 
     { 
     "type":"file", 
     "id":"22887167395", 
     "sequence_id":"0", 
     "etag":"0", 
     "sha1":"883c99863eefc0f46b3d34915cc4d97a6008fabf", 
     "name":"13.ppt" 
     }, 
     { 
     "type":"file", 
     "id":"22887169687", 
     "sequence_id":"0", 
     "etag":"0", 
     "sha1":"a345fd68b1c90a3678a3e746e0e5343693d8a022", 
     "name":"6.docx" 
     } 
    ], 
    "offset":0, 
    "limit":100, 
    "order": 
    [ 
     { 
     "by":"type", 
     "direction":"ASC" 
     }, 
     { 
     "by":"name", 
     "direction":"ASC" 
     } 
    ] 
    } 
} 

基本上,這是根文件夾(在這種情況下),其中包含兩個文件:
13.ppt
6.docx
我創建的類:

[JsonObject(MemberSerialization.OptIn)] 
public class BoxFile 
{ 
    [JsonProperty(PropertyName = "type")] 
    public string Type { get; internal set; } 

    [JsonProperty(PropertyName = "id")] 
    public string Id { get; internal set; } 

    [JsonProperty(PropertyName = "sequence_id")] 
    public string SequenceId { get; internal set; } 

    [JsonProperty(PropertyName = "etag")] 
    public string Etag { get; internal set; } 

    [JsonProperty(PropertyName = "name")] 
    public string Name { get; internal set; } 

    [JsonProperty(PropertyName = "created_at")] 
    public string CreatedAt { get; internal set; } 

    [JsonProperty(PropertyName = "modified_at")] 
    public string ModifiedAt { get; internal set; } 

    [JsonProperty(PropertyName = "description")] 
    public string Description { get; internal set; } 

    [JsonProperty(PropertyName = "size")] 
    public long Size { get; internal set; } 

    [JsonProperty(PropertyName = "item_collection")] 
    public IEnumerable<BoxFile> ItemCollection { get; internal set; } 
} 

但「item_collection」部分沒有工作..它給我一個錯誤..

怎麼辦我在「item_collection」內獲得子文件列表?

我用它由:

private T ParseJson<T>(string json) where T : class, new() 
    { 
      JObject jobject = JObject.Parse(json); 
      return JsonConvert.DeserializeObject<T>(jobject.ToString()); 
    } 

和:

BoxFile parsed = ParseJson<BoxFile>(json); 
+0

指定語言標籤請 – 2014-11-24 10:27:24

+0

你的意思是c#? – user990635 2014-11-24 11:33:05

+0

有關錯誤的事情是它們通常包含用於調試的有用信息。所以不要說「它給我一個錯誤」。相反,請描述錯誤,複製並粘貼單詞。告訴我們它是否有編譯錯誤或運行時錯誤等 – Chris 2014-11-24 11:53:45

回答

1

你得到一個錯誤,因爲你的階級結構不符合您的JSON。具體來說,在JSON中,item_collection屬性是一個對象,而不是一個列表。該JSON對象有兩個屬性,total_countentries,後者包含實際的文件列表。爲了解決這個問題,你需要定義另一個類:

public class ItemCollection 
{ 
    [JsonProperty(PropertyName = "entries")] 
    public IEnumerable<BoxFile> Entries { get; internal set; } 
} 

,然後更改ItemCollection財產在你BoxFile類中使用這個新類:

[JsonProperty(PropertyName = "item_collection")] 
    public ItemCollection ItemCollection { get; internal set; } 

然後,您可以訪問諸如文件列表這樣的:

BoxFile parsed = ParseJson<BoxFile>(json); 

foreach (BoxFile file in parsed.ItemCollection.Entries) 
{ 
    Console.WriteLine(file.Name); 
} 

這裏是一個工作演示:https://dotnetfiddle.net/DB9Coc

另外,您可以將ParseJson方法簡化爲一行。沒有必要將JSON解析爲JObject,將其重新轉換爲JSON,然後再解析它。

private T ParseJson<T>(string json) where T : class, new() 
{ 
    return JsonConvert.DeserializeObject<T>(json); 
}