如何在 C# 中解析 JsonObject



我正在尝试解析下面的json对象,但我不知道该怎么做 因为 json 对象中的"讲师"? 我不知道我该如何管理"讲师",Json 中还有另一个对象 但是在data[0]中它是一个对象,在data[1]中它是一个标志或布尔值。 这件事让我很困惑.任何想法我如何实现这个???

{
    "result": "Success",
    "data": [
        {
            "student_course_id": "82",
            "student_id": "30",
            "term_course_id": "18",
            "section_id": "3",
            "term_id": "12",
            "course_id": "15",
            "credit_hours": "26",
            "is_elective": "Elective",
            "is_practical": "0",
            "teacher_id": "10",
            "program_id": "5",
            "course_code": "E2",
            "course_title": "English 2",
            "lecturer": {
                "fname": "Ali",
                "lname": "farooq",
                "phone": "1234567890",
                "email": "farooq@teacher.com",
                "thumb": "../photos/thumb/1391515491.png"
            }
        },
        {
            "student_course_id": "83",
            "student_id": "30",
            "term_course_id": "19",
            "section_id": "3",
            "term_id": "12",
            "course_id": "16",
            "credit_hours": "26",
            "is_elective": "Elective",
            "is_practical": "0",
            "teacher_id": "8",
            "program_id": "5",
            "course_code": "C2",
            "course_title": "Culture 2",
            "lecturer": false
        }
        ]
}

C# 代码

public async static Task<StudentSubjectsClassWithError> StudentSubjectsList()
{
    HttpClient client = new HttpClient();
    string baseUrl = getBaseUrl();
    try
    {
        string flickrResult = await client.GetStringAsync(baseUrl);
        StudentSubjectsClassWithError studentSubjectsResult = new StudentSubjectsClassWithError();
        try
        {
            StudentSubjectsJson apiData =
                JsonConvert.DeserializeObject<StudentSubjectsJson>(flickrResult);
            List<StudentSubjectsClass> mStudentSubjectsList = new List<StudentSubjectsClass>();
            if (apiData.data != null && apiData.result == "Success")
            {
                studentSubjectsResult.message = "";
                studentSubjectsResult.result = apiData.result;
                foreach (StudentSubjectsJsonItem data in apiData.data)
                {
                    StudentSubjectsClass studentSubjects = new StudentSubjectsClass();
                    studentSubjects.student_course_id = data.student_course_id;
                    studentSubjects.student_id = data.student_id;
                    studentSubjects.term_course_id = data.term_course_id;
                    studentSubjects.section_id = data.section_id;
                    studentSubjects.term_id = data.term_id;
                    studentSubjects.course_id = data.course_id;
                    studentSubjects.credit_hours = data.credit_hours;
                    studentSubjects.is_elective = data.is_elective;
                    studentSubjects.is_practical = data.is_practical;
                    studentSubjects.program_id = data.program_id;
                    studentSubjects.course_code = data.course_code;
                    studentSubjects.course_title = data.course_title;
                   //// studentSubjects.lecturer ?????
                    mStudentSubjectsList.Add(studentSubjects);
                }
            }
            else
            {
                studentSubjectsResult.result = apiData.result;
                studentSubjectsResult.message = "No records found.";
            }
            studentSubjectsResult.studentSubjectsList = mStudentSubjectsList;
            return studentSubjectsResult;
        }
        catch (JsonSerializationException)
        {
            try
            {
                StudentSubjectsErrorJson apiData =
                   JsonConvert.DeserializeObject<StudentSubjectsErrorJson>(flickrResult);
                studentSubjectsResult.result = apiData.result;
                studentSubjectsResult.message = apiData.data;
                studentSubjectsResult.studentSubjectsList = null;
                return studentSubjectsResult;
            }
            catch (JsonSerializationException)
            {
            }

        }
        return null;
    }
    catch (Exception)
    {
        return null;
     //   MessageBox.Show("Internet Connection Problem");
    }
}

> 我正在使用Visual Studio 2012,当我想将Json字符串转换为C#类对象时,我复制了Json字符串和

-> Click Edit
-> Click Paste Special
-> Click Paste JSON as Classes

或者您可以使用乔纳森·基思(Jonathan Keith)的出色在线工具 http://json2csharp.com/

使用 json2csharp 我从您的 JSON 中获得了这个类

public class Datum
{
    public string student_course_id { get; set; }
    public string student_id { get; set; }
    public string term_course_id { get; set; }
    public string section_id { get; set; }
    public string term_id { get; set; }
    public string course_id { get; set; }
    public string credit_hours { get; set; }
    public string is_elective { get; set; }
    public string is_practical { get; set; }
    public string teacher_id { get; set; }
    public string program_id { get; set; }
    public string course_code { get; set; }
    public string course_title { get; set; }
    public object lecturer { get; set; }
}
public class RootObject
{
    public string result { get; set; }
    public List<Datum> data { get; set; }
}

编辑

我还注意到你的数据数组将讲师作为对象,第二个数据将讲师作为布尔值。如果讲师不存在,您可以通过简单地不包括讲师来解决此问题。这会将类更改为:

public class Lecturer
{
    public string fname { get; set; }
    public string lname { get; set; }
    public string phone { get; set; }
    public string email { get; set; }
    public string thumb { get; set; }
}
public class Datum
{
    public string student_course_id { get; set; }
    public string student_id { get; set; }
    public string term_course_id { get; set; }
    public string section_id { get; set; }
    public string term_id { get; set; }
    public string course_id { get; set; }
    public string credit_hours { get; set; }
    public string is_elective { get; set; }
    public string is_practical { get; set; }
    public string teacher_id { get; set; }
    public string program_id { get; set; }
    public string course_code { get; set; }
    public string course_title { get; set; }
    public Lecturer lecturer { get; set; }
}
public class RootObject
{
    public string result { get; set; }
    public List<Datum> data { get; set; }
}
顶级

StudentSubjectsJsondata数组中的每个StudentSubjectsJsonItem

对于讲师,您需要定义StudentSubjectsJsonLecturer并将 set is 作为称为lecturerStudentSubjectsJsonItem的属性。

例如:

public class StudentSubjectsJsonItem {
    //Existing properties
    public StudentSubjectsJsonLecturer lecturer {get;set;}
}
public class StudentSubjectsJsonLecturer {
    public string fname {get;set;}
    public string lname {get;set;}
    //And so on...
}

由于您已经在复制属性(您不应该这样做,这就是反序列化的目的),您可以将数据反序列化为dynamic对象(Newtonsoft Json v4+):

dynamic apiData = JsonConvert.DeserializeObject<StudentSubjectsJson>(flickrResult);

并继续处理其余的代码,然后当您必须处理"讲师"时,检查它是否像往常一样"错误":

if (apiData.data[x].lecturer != false){
...
}

免责声明:我还没有编译这个来尝试它,这是一个建议尝试什么

我建议您使用第三方 json 库,而不是自己编写一个。尤其是这个:点击下载

它是一个单文件 json 库。

相关内容

  • 没有找到相关文章

最新更新