0

我正在尝试从 json 中提取一些数据。我一直在 JSS 或 Json.net 中寻找解决方案,但无法解决这个问题。这就是我的 Json 的样子: 注意:我已经测试过并且映射和去中心化工作!我正在寻找一种从 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 中提取以下信息。(来自 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);

}
4

1 回答 1

2

您可以使用 LINQ 快速查询对象。

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

测试任务,如果为 null 则没有匹配 id 的任务。如果您确定会有匹配的任务,您可以使用 .First(),但如果列表中没有匹配项,它会抛出异常

您需要添加一个 using System.Linq; 如果你还没有那个。

于 2013-08-07T04:57:15.330 回答