gpt4 book ai didi

c# - 如何在 C# 中解析 JsonObject

转载 作者:太空宇宙 更新时间:2023-11-03 20:08:06 25 4
gpt4 key购买 nike

我正在尝试解析下面的 json 对象,但由于 json 对象中的“讲师”,我不知道该怎么做?我不知道如何管理“讲师”,他们是 Json 中的另一个对象,但在 data[0] 中它是一个对象,在 data[1] 中它是一个标志或 Boolean 。这件事让我很困惑。知道我怎么能做到这一点吗???

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

编辑

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

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

关于c# - 如何在 C# 中解析 JsonObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21734103/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com