2013-08-07 138 views
0

我想從json中提取一些數據。我一直在JSS或Json.net上尋找解決方案,但一直無法解決這個問題。這是我的Json的樣子: 注:我已經測試過,測繪和分散工作!我正在尋找一種方法來從json中提取特定的數據!從asp.net c中提取json數據#

在此先感謝!

{ 
"tasks":[ 
    { 
    "id":"tmp_fk1345624806538", 
    "name":"Gantt editor ", 
    "code":"", 
    "level":0, 
    "status":"STATUS_ACTIVE", 
    "start":1346623200000, 
    "duration":5, 
    "end":1347055199999, 
    "startIsMilestone":false, 
    "endIsMilestone":false, 
    "assigs":[ 
     { 
     "resourceId":"tmp_3", 
     "id":"tmp_1345625008213", 
     "roleId":"tmp_1", 
     "effort":7200000 
     } 
    ], 
    "depends":"", 
    "description":"", 
    "progress":0 
    }, 
    { 
    "id":"tmp_fk1345624806539", 
    "name":"phase 1", 
    "code":"", 
    "level":1, 
    "status":"STATUS_ACTIVE", 
    "start":1346623200000, 
    "duration":2, 
    "end":1346795999999, 
    "startIsMilestone":false, 
    "endIsMilestone":false, 
    "assigs":[ 
     { 
     "resourceId":"tmp_1", 
     "id":"tmp_1345624980735", 
     "roleId":"tmp_1", 
     "effort":36000000 
     } 
    ], 
    "depends":"", 
    "description":"", 
    "progress":0 
    }, 
    { 
    "id":"tmp_fk1345624789530", 
    "name":"phase 2", 
    "code":"", 
    "level":1, 
    "status":"STATUS_SUSPENDED", 
    "start":1346796000000, 
    "duration":3, 
    "end":1347055199999, 
    "startIsMilestone":false, 
    "endIsMilestone":false, 
    "assigs":[ 
     { 
     "resourceId":"tmp_2", 
     "id":"tmp_1345624993405", 
     "roleId":"tmp_2", 
     "effort":36000000 
     } 
    ], 
    "depends":"2", 
    "description":"", 
    "progress":0 
    } 
], 
"resources":[ 
    { 
    "id":"tmp_1", 
    "name":"Resource 1" 
    }, 
    { 
    "id":"tmp_2", 
    "name":"Resource 2" 
    }, 
    { 
    "id":"tmp_3", 
    "name":"Resource 3" 
    } 
],"roles":[ 
{ 
    "id":"tmp_1", 
    "name":"Project Manager" 
}, 
{ 
    "id":"tmp_2", 
    "name":"Worker" 
} 
], 
"canWrite":true, 
"canWriteOnParent":true, 
"selectedRow":0, 
"deletedTaskIds":[], 
} 

我已經映射爲遵循

public class Rootobject 
{ 
    public Task[] tasks { get; set; } 
    public Resource[] resources { get; set; } 
    public Role[] roles { get; set; } 
    public bool canWrite { get; set; } 
    public bool canWriteOnParent { get; set; } 
    public int selectedRow { get; set; } 
    public object[] deletedTaskIds { get; set; } 
} 

public class Task 
{ 
    public string id { get; set; } 
    public string name { get; set; } 
    public string code { get; set; } 
    public int level { get; set; } 
    public string status { get; set; } 
    public long start { get; set; } 
    public int duration { get; set; } 
    public long end { get; set; } 
    public bool startIsMilestone { get; set; } 
    public bool endIsMilestone { get; set; } 
    public Assig[] assigs { get; set; } 
    public string depends { get; set; } 
    public string description { get; set; } 
    public int progress { get; set; } 
} 

public class Assig 
{ 
    public string resourceId { get; set; } 
    public string id { get; set; } 
    public string roleId { get; set; } 
    public int effort { get; set; } 
} 

public class Resource 
{ 
    public string id { get; set; } 
    public string name { get; set; } 
} 

public class Role 
{ 
    public string id { get; set; } 
    public string name { get; set; } 
} 

,我需要提取以下從我的JSON信息(從5月JSON特定任務,例如第一個id爲:tmp_fk1345624806538)。 。 注:我得到我的JSON從JSON文件如下:

string startDate; // this is what i need to extract 
string endDate; // this is what i need to extract 
string Progress; // this is what i need to extract 

public void load() 
{ 
    GC.GClass l = new GC.GClass(); 
    string jsonString = l.load(); // i get my json from a json file 

    Rootobject project = JsonConvert.DeserializeObject<Rootobject>(jsonString); 

} 

回答

2

您可以使用LINQ快速查詢的對象。

Task task = project.tasks.FirstOrDefault(t=> t.id == "tmp_fk1345624806538"); 

測試任務,如果爲null,則表示沒有與id匹配的任務。如果你確定會有匹配的任務,你可以使用.First(),但是如果列表中沒有匹配,它會拋出一個異常。

你需要添加一個using System.Linq;如果你還沒有。