2017-02-28 28 views
0

問題是,獲取所有組的Gerrit響應看起來像這樣,我發現擁有{}之外的組名稱會給我造成問題,因爲當我創建類來保存該組,我不得不讓組中的類能夠反序列化響應。通用類保存gerrit的JSON數據

但是,無論何時創建一個新的gerrit組,我都不想創建一個具有該名稱的新類。

[{ 
"Administrators": { 
    "id": "6a1e70e1a88782771a91808c8af9bbb7a9871389", 
    "url": "#/admin/groups/uuid-6a1e70e1a88782771a91808c8af9bbb7a9871389", 
    "options": { 
    }, 
    "description": "Gerrit Site Administrators", 
    "group_id": 1, 
    "owner": "Administrators", 
    "owner_id": "6a1e70e1a88782771a91808c8af9bbb7a9871389" 
}, 
"Anonymous Users": { 
    "id": "global%3AAnonymous-Users", 
    "url": "#/admin/groups/uuid-global%3AAnonymous-Users", 
    "options": { 
    }, 
    "description": "Any user, signed-in or not", 
    "group_id": 2, 
    "owner": "Administrators", 
    "owner_id": "6a1e70e1a88782771a91808c8af9bbb7a9871389" 
}, 
"MyProject_Committers": { 
    "id": "834ec36dd5e0ed21a2ff5d7e2255da082d63bbd7", 
    "url": "#/admin/groups/uuid-834ec36dd5e0ed21a2ff5d7e2255da082d63bbd7", 
    "options": { 
    "visible_to_all": true, 
    }, 
    "group_id": 6, 
    "owner": "MyProject_Committers", 
    "owner_id": "834ec36dd5e0ed21a2ff5d7e2255da082d63bbd7" 
}] 

這裏是類:

public class Administrators 
{ 
    public string id { get; set; } 
    public string url { get; set; } 
    public Options options { get; set; } 
    public string description { get; set; } 
    public int group_id { get; set; } 
    public string owner { get; set; } 
    public string owner_id { get; set; } 
} 

public class AnonymousUsers 
{ 
    public string id { get; set; } 
    public string url { get; set; } 
    public options { get; set; } 
    public string description { get; set; } 
    public int group_id { get; set; } 
    public string owner { get; set; } 
    public string owner_id { get; set; } 
} 

public class MyProjectCommitters 
{ 
    public string id { get; set; } 
    public string url { get; set; } 
    public options { get; set; } 
    public int group_id { get; set; } 
    public string owner { get; set; } 
    public string owner_id { get; set; } 
} 

和我想要做的是有一個叫GerritGroup類來保存這一切並能夠反序列化的任何這樣的組中的對象。

我現在做的方式是這樣的:

IList<GerritGroup> grps = JsonConvert.DeserializeObject<IList<GerritGroup>>(cleanedResponse); 

,我也得到一個列表與空的所有屬性一個對象。

你能給我一些關於這個JSON響應的建議嗎?

+0

可能使用http://jsonlint.com/來驗證json,例如「visible_to_all」:true *,* - 逗號可能是解析錯誤的原因。 – Vladimir

回答

1

你已經有了一個集合,它擁有一個帶有鍵值對的對象,其中鍵是可變的,值是組。

換句話說,你只需要一個Group類:

public class Group 
{ 
    public string id { get; set; } 
    public string url { get; set; } 
    public Dictionary<string, string> options { get; set; } 
    public string description { get; set; } 
    public int group_id { get; set; } 
    public string owner { get; set; } 
    public string owner_id { get; set; } 
} 

而你需要反序列化JSON作爲List<Dictionary<string, Group>>

var groups = JsonConvert.DeserializeObject<List<Dictionary<string, Group>>>(json); 

foreach (var g in groups) 
{ 
    foreach (var e in g) 
    { 
     Console.WriteLine(e.Key + " " + e.Value.description); 
    } 
} 
+0

事實上,這是我不明白的。非常感謝你!它像一個魅力! – UrsulRosu