在 C# 中遍历 json 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18333512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:57:14  来源:igfitidea点击:

Iterate through json object in c#

c#jsonjson.net

提问by Poorya

I' Want to create a loop to check a condition on my Json object. I'm looking for a way to iterate through my json object:

我想创建一个循环来检查我的 Json 对象的条件。我正在寻找一种方法来遍历我的 json 对象:

Json:

杰森:

{"tasks":[
        {
            "id":-1,
            "name":"Gantt editor",
            "code":"",
            "level":0,
            "status":"STATUS_ACTIVE",
            "start":1372608000000,
            "duration":21,
            "end":1375113599999,
            "startIsMilestone":true,
            "endIsMilestone":false,
            "collapsed":false,
            "assigs":[]
        },
        {
            "id":"tmp_fk1372575559620",
            "name":"release",
            "code":"",
            "level":1,
            "status":"STATUS_ACTIVE",
            "start":1372608000000,
            "duration":1,
            "end":1372694399999,
            "startIsMilestone":false,
            "endIsMilestone":false,
            "collapsed":false,
            "assigs":[]
        }
        ],       // number of tasks may vary
"selectedRow":8,
"deletedTaskIds":[],
"resources":
    [
        {
        "id":"tmp_1",
        "name":"Resource 1"
        }
    ],
"roles":[
        {
            "id":"tmp_1",
            "name":"Project Manager"
        }
        ],
"canWrite":true,
"canWriteOnParent":true
}

I know how to map it so let's assume i mapped Task and RootObject as follow:

我知道如何映射它,所以让我们假设我映射 Task 和 RootObject 如下:

public class Task
{
    public object id { get; set; }
    public string name { get; set; }
    public string code { get; set; }
    public int level { get; set; }
    public string status { get; set; }
    public object start { get; set; }
    public int duration { get; set; }
    public object end { get; set; }
    public bool startIsMilestone { get; set; }
    public bool endIsMilestone { get; set; }
    public bool collapsed { get; set; }
    public List<object> assigs { get; set; }
}
public class RootObject
{
    public List<Task> tasks { get; set; }
    public int selectedRow { get; set; }
    public List<object> deletedTaskIds { get; set; }
    public List<Resource> resources { get; set; }
    public List<Role> roles { get; set; }
    public bool canWrite { get; set; }
    public bool canWriteOnParent { get; set; }
} // And etc .....

I know how to check the tasks manually for example for first one

我知道如何手动检查任务,例如第一个

Rootobject project = JsonConvert.DeserializeObject<Rootobject>(jsonString);
    Task task = project.tasks.FirstOrDefault(t => t.id == "-1");
    decimal start = Convert.ToDecimal(task.start);
    decimal end = Convert.ToDecimal(task.end);
    decimal prog = Convert.ToDecimal(task.progress);

and then use task to check all it's attributes

然后使用任务来检查它的所有属性

How can I check all tasks ?

如何检查所有任务?

Thanks In Advance !

提前致谢 !

采纳答案by Alexey

If you want to iterate through all tasks, you can use:

如果要遍历所有任务,可以使用:

foreach (var task in project.tasks)
{
    // do some stuff
}

or you can use LINQ to filter them, something like this:

或者您可以使用 LINQ 来过滤它们,如下所示:

foreach (var task in project.tasks.Where(t => t.id == "-1"))
{
    // do some stuff
}

which is basically same with your example, with only difference that Where returns IEnumerable and not just Task like FirstOrDefault in your example.

这与您的示例基本相同,唯一的区别是 Where 返回 IEnumerable 而不仅仅是像您示例中的 FirstOrDefault 这样的 Task 。