2017-04-20 83 views
0

我正在使用JsonClassGenerator項目將Json轉換爲C#類。用JObject []篩選和匹配奮鬥

這是一些中間件我建立勾到TrackVia API項目我公司已建成了很多英寸

現在我建立了數據層在我的.net應用程序中

基本上,我有一個Json結構,我需要解析以確定是否應該根據「結構」中的「canUpdate」值序列化「數據」中的某個屬性。

讓我們開始的JSON結構:

{ 
    "structure": [ 
    { 
     "name": "Updated", 
     "type": "datetime", 
     "required": false, 
     "unique": false, 
     "canRead": true, 
     "canUpdate": false, 
     "canCreate": false 
    }, 
    { 
     "name": "Amount", 
     "type": "currency", 
     "required": false, 
     "unique": false, 
     "canRead": true, 
     "canUpdate": false, 
     "canCreate": false 
    }, 
    { 
     "name": "Description", 
     "type": "paragraph", 
     "required": false, 
     "unique": false, 
     "canRead": true, 
     "canUpdate": false, 
     "canCreate": false 
    }, 
    { 
     "name": "Created", 
     "type": "datetime", 
     "required": false, 
     "unique": false, 
     "canRead": true, 
     "canUpdate": false, 
     "canCreate": false 
    }, 
    { 
     "name": "Quantity", 
     "type": "number", 
     "required": false, 
     "unique": false, 
     "canRead": true, 
     "canUpdate": true, 
     "canCreate": true 
    }, 
    { 
     "name": "Created By User", 
     "type": "user", 
     "required": false, 
     "unique": false, 
     "canRead": true, 
     "canUpdate": false, 
     "canCreate": false 
    }, 
    { 
     "name": "ID", 
     "type": "number", 
     "required": false, 
     "unique": true, 
     "canRead": true, 
     "canUpdate": false, 
     "canCreate": false 
    }, 
    { 
     "name": "Last User", 
     "type": "user", 
     "required": false, 
     "unique": false, 
     "canRead": true, 
     "canUpdate": false, 
     "canCreate": false 
    }, 
    { 
     "name": "Unit_Tracker", 
     "type": "relationship", 
     "required": false, 
     "unique": false, 
     "canRead": true, 
     "canUpdate": true, 
     "canCreate": true 
    }, 
    { 
     "name": "Project_Materials", 
     "type": "relationship", 
     "required": false, 
     "unique": false, 
     "canRead": true, 
     "canUpdate": true, 
     "canCreate": true 
    }, 
    { 
     "name": "Record ID", 
     "type": "identifier", 
     "required": false, 
     "unique": false, 
     "canRead": true, 
     "canUpdate": false, 
     "canCreate": false 
    } 
    ], 
    "data": [ 
    { 
     "Updated": "2017-04-20T10:12:00.000-06:00", 
     "Project_Materials(id)": 404, 
     "Unit_Tracker(id)": 209, 
     "Amount": "78.18", 
     "Description": "1/2 in", 
     "Quantity": "1", 
     "Created By User": "By", 
     "Unit_Tracker": "R04-5050", 
     "Project_Materials": "1001755734", 
     "id": 772, 
     "Created": "2017-04-20T10:12:00.000-06:00", 
     "Last User": "By", 
     "Last User(id)": 58443, 
     "Created By User(id)": 58443, 
     "Record ID": "1/2 in" 
    } 
    ], 
    "totalCount": 763 
} 

這就是我需要匹配。我需要將「數據」中的一個鍵(即「更新」)與「結構」內的鍵值(即結構 - >「名稱」:「更新」)進行匹配,然後確定「canRead」,「canUpdate」,「canCreate」設置某種標誌。

的標誌,那麼將告訴Codewriter添加一行「[JsonIgnoreSerialization]」而產生的輸出我的文本格式化的屬性如下所示:

 [JsonIgnoreSerialization] 
     [JsonProperty("Updated")] 
     public string Updated { get; set; } 

我還沒有與JObjects工作,所以我的此時代碼爲空,因爲我不知道從哪裏開始。

這是一個大問題,這是因爲我必須根據從他們的API導出的Json將TrackAdvisor中的數百個錶轉換爲我的應用中的C#類 - 這需要幾周的時間,並且希望進行自定義轉換工具,爲我建立我的C#類 - 所以它將是盛大的。

不尋找講義,我願意爲自己的成功而工作,需要一些方向。

+0

你覺得答案有用嗎?或者它沒有解決你的問題? – Sameer

回答

0

試試這個自定義類結構

public class Root 
{ 
    public List<Structure> structure; 
} 

public class Structure 
{ 
    public string name { get; set; } 
    public string type { get; set; } 
    public string required { get; set; } 
    public string unique { get; set; } 
    public string canRead { get; set; } 
    public string canUpdate { get; set; } 
    public string canCreate { get; set; } 
} 

,並使用以下代碼訪問canUpdate財產。

using (var stream = new StreamReader("sample.json")) 
{ 

    var rootObject = JsonConvert.DeserializeObject<Root>(stream.ReadToEnd()); 
    int index = 0; 
    foreach (var structures in rootObject.structure) 
    { 
      Console.WriteLine(structures.canUpdate); 
    } 
} 

注: sample.json包含您的JSON響應。