从asp.net c#中的json提取数据 [英] extract data from json in asp.net c#

查看:76
本文介绍了从asp.net c#中的json提取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从json提取一些数据.我一直在JSS或Json.net中寻找解决方案,但一直无法解决这个问题.这是我的Json的样子: 注意:我已经测试过,映射和分散操作也可以!我正在寻找一种从json提取特定数据的方法!

I'm trying to extract some data from json. I've been looking for a solution either in JSS or Json.net but haven't been able to figure this problem out. this is how my Json looks like: Note: i Have tested and the mapping and decentralization works! I'm looking for a way to extract specifc data from the 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中提取以下信息.(例如,可以从may json中的特定Task!中提取第一个ID为tmp_fk1345624806538的信息). 注意:我正在从json文件中获取json,如下所示:

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 ). Note: i'm getting my json from a json file as follow:

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

}

推荐答案

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

You can use LINQ to query the object quickly.

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

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

Test task, and if null then there was not task with matching 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

您需要添加一个使用System.Linq;如果还没有的话.

You'll need to add a using System.Linq; if you don't have that already.

这篇关于从asp.net c#中的json提取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆