如何在List <class>?</class>中解析这个json

时间:2014-07-25 10:39:52

标签: c# .net json.net

我正在开发框架4.0中的Windows窗体应用程序。我正在使用JSON.NET。我在字符串变量中有以下json。我在json中有10个类如下。如何解析以下json并将其转换为List

[
        {
            "Class": 1,
            "ClassUrl": "EngineeringSemister12014A",
            "OEPTitle": "Engineer",
            "Subject": [
                {
                    "Id": 1,
                    "OEPTitle": "Algebra"
                },
                {
                    "Id": 2,
                    "OEPTitle": "Maths"
                }
            ],
            "IsArchived": true
        },
        {
            "Class": 7,
            "ClassUrl": "Engineer2014A",
            "OEPTitle": "Engineer",
            "Subject": [
                {
                    "Id": 1,
                    "OEPTitle": "Algebra"
                },
                {
                    "Id": 2,
                    "OEPTitle": "Maths"
                }
            ]
        },
        {
            "Class": 8,
            "ClassUrl": "Engineer22014A",
            "OEPTitle": "Engineer2",
            "Subject": [
                {
                    "Id": 1,
                    "OEPTitle": "Algebra"
                },
                {
                    "Id": 2,
                    "OEPTitle": "Maths"
                }
            ]
        },
        {
            "Class": 9,
            "ClassUrl": "Engineer32014A",
            "OEPTitle": "Engineer3",
            "Subject": [
                {
                    "Id": 1,
                    "OEPTitle": "Algebra"
                },
                {
                    "Id": 2,
                    "OEPTitle": "Maths"
                }
            ]
        },
        {
            "Class": 10,
            "ClassUrl": "Engineer32014B",
            "OEPTitle": "Engineer3",
            "Subject": [
                {
                    "Id": 1,
                    "OEPTitle": "Algebra"
                },
                {
                    "Id": 2,
                    "OEPTitle": "Maths"
                },
                {
                    "Id": 8,
                    "OEPTitle": "english"
                }
            ],
            "IsRecycled": true
        },
        {
            "Class": 12,
            "ClassUrl": "Engineer52014B",
            "OEPTitle": "Engineer5",
            "Subject": [
                {
                    "Id": 1,
                    "OEPTitle": "Algebra"
                },
                {
                    "Id": 2,
                    "OEPTitle": "Maths"
                },
                {
                    "Id": 8,
                    "OEPTitle": "english"
                }
            ]
        },
        {
            "Class": 13,
            "ClassUrl": "Engineer62014B",
            "OEPTitle": "Engineer6",
            "Subject": [
                {
                    "Id": 1,
                    "OEPTitle": "Algebra"
                },
                {
                    "Id": 2,
                    "OEPTitle": "Maths"
                },
                {
                    "Id": 8,
                    "OEPTitle": "english"
                }
            ]
        },
        {
            "Class": 16,
            "ClassUrl": "Mechanical2014undefined",
            "OEPTitle": "Mechanical Branch",
            "Subject": [
                {
                    "Id": 6,
                    "OEPTitle": "history"
                },
                {
                    "Id": 4,
                    "OEPTitle": "chemical "
                }
            ]
        },
        {
            "Class": 6,
            "ClassUrl": "Engineer12014A",
            "OEPTitle": "Engineer1",
            "Subject": [
                {
                    "Id": 1,
                    "OEPTitle": "Algebra"
                },
                {
                    "Id": 2,
                    "OEPTitle": "Maths"
                },
                {
                    "Id": 6,
                    "OEPTitle": "history"
                },
                {
                    "Id": 4,
                    "OEPTitle": "chemical "
                }
            ]
        },
        {
            "Class": 11,
            "ClassUrl": "Engineer42014B",
            "OEPTitle": "Engineer4",
            "Subject": [
                {
                    "Id": 1,
                    "OEPTitle": "Algebra"
                },
                {
                    "Id": 2,
                    "OEPTitle": "Maths"
                },
                {
                    "Id": 6,
                    "OEPTitle": "history"
                },
                {
                    "Id": 4,
                    "OEPTitle": "chemical "
                },
                {
                    "Id": 8,
                    "OEPTitle": "english"
                }
            ]
        }
    ]

1 个答案:

答案 0 :(得分:5)

使用json2csharp

public class Subject
{
    public int Id { get; set; }
    public string OEPTitle { get; set; }
}

public class RootObject
{
    public int Class { get; set; }
    public string ClassUrl { get; set; }
    public string OEPTitle { get; set; }
    public List<Subject> Subject { get; set; }
    public bool IsArchived { get; set; }
}

现在,您可以使用JSON.NET:

List<RootObject> rootObj = JsonConvert.Deserialize<List<RootObject>>(yourJsonHere);