简体   繁体   English

从asp.net中的json中提取数据c#

[英]extract data from json in asp.net c#

I'm trying to extract some data from json. 我正试图从json中提取一些数据。 I've been looking for a solution either in JSS or Json.net but haven't been able to figure this problem out. 我一直在寻找JSS或Json.net的解决方案,但一直无法解决这个问题。 this is how my Json looks like: Note: i Have tested and the mapping and decentralization works! 这就是我的Json的样子: 注意:我已经过测试,映射和分散工作! I'm looking for a way to extract specifc data from the json! 我正在寻找一种从json中提取特定数据的方法!

Thanks in Advance! 提前致谢!

{
"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":[],
}

i've already mapped as follow 我已经映射如下

 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; }
}

and I need to extract following information from my json.(from specific Task in may json! for example the first one with id : tmp_fk1345624806538 ). 我需要从我的json中提取以下信息。(来自may json中的特定任务!例如第一个id为:tmp_fk1345624806538)。 Note: i'm getting my json from a json file as follow: 注意:我从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);

}

You can use LINQ to query the object quickly. 您可以使用LINQ快速查询对象。

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

Test task, and if null then there was not task with matching id. 测试任务,如果为null则没有匹配id的任务。 If you are sure that there will be a matching task your can just use .First(), but it will throw an exception if there is no match in the list 如果您确定将有匹配的任务,您可以使用.First(),但如果列表中没有匹配则会抛出异常

You'll need to add a using System.Linq; 你需要添加一个使用System.Linq; if you don't have that already. 如果你还没有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM