[英]Convert Gson Array to Arraylist
我已經看到了很多這樣的問題,但是我仍然無法解決這個問題。
我從服務器收到了一個json,其中包含多個gson數組。
我如何反序列化來自服務器的響應以滿足這些模型。
主題模型:
public class Subject {
public int SubjectId;
public String SubjectName;
public ArrayList Courses;
}
課程模式:
public class Course {
public String CourseName;
public String CourseDescription;
public int CourseId;
public int Subject_SubjectId;
}
編輯這是服務器返回的內容:我使用了Google Chrome Extension PostMan來檢索它。
這是實際的
{
"StudentSubject": [
{
"SubjectId": 1059,
"SubjectName": "Accounting",
"Student_CourseId": 0,
"UniversitySubjectId": 0,
"Courses": [
{
"CourseId": 1091,
"CourseName": "ACCT 101",
"CourseDescription": "",
"Subject_SubjectId": 1059
},
{
"CourseId": 1092,
"CourseName": "ACCT 111",
"CourseDescription": "",
"Subject_SubjectId": 1059
},
{
"CourseId": 1093,
"CourseName": "ACCT 115",
"CourseDescription": "Financial Accounting Foundations",
"Subject_SubjectId": 1059
}
]
},
{
"SubjectId": 1060,
"SubjectName": "Mathematics",
"Student_CourseId": 0,
"UniversitySubjectId": 0,
"Courses": [
{
"CourseId": 1094,
"CourseName": "MATH 100",
"CourseDescription": "Fundamentals of Mathematics",
"Subject_SubjectId": 1060
},
{
"CourseId": 1095,
"CourseName": "MATH 101",
"CourseDescription": "Introduction to Analysis I",
"Subject_SubjectId": 1060
},
{
"CourseId": 2126,
"CourseName": "MATH 200",
"CourseDescription": "Multivariate Calculus",
"Subject_SubjectId": 1060
},
{
"CourseId": 2132,
"CourseName": "MATH 102",
"CourseDescription": "Introduction to Analysis II",
"Subject_SubjectId": 1060
}
]
},
{
"SubjectId": 1069,
"SubjectName": "Bioscience & Biotechnology",
"Student_CourseId": 0,
"UniversitySubjectId": 0,
"Courses": [
{
"CourseId": 1109,
"CourseName": "BIO 100",
"CourseDescription": "Applied Cells, Genetics & Physiology",
"Subject_SubjectId": 1069
},
{
"CourseId": 2123,
"CourseName": "BIO 124",
"CourseDescription": "Evolution & Organismal Diversity",
"Subject_SubjectId": 1069
}
]
},
{
"SubjectId": 2084,
"SubjectName": "Computer Science",
"Student_CourseId": 0,
"UniversitySubjectId": 0,
"Courses": [
{
"CourseId": 2137,
"CourseName": "CS 101",
"CourseDescription": "",
"Subject_SubjectId": 2084
}
]
},
{
"SubjectId": 2086,
"SubjectName": "Business Statistics",
"Student_CourseId": 0,
"UniversitySubjectId": 0,
"Courses": [
{
"CourseId": 2141,
"CourseName": "STAT 101",
"CourseDescription": "",
"Subject_SubjectId": 2086
}
]
}
]
}
嘗試
Type listType = new TypeToken<List<Subject>>() {}.getType();
List<Subject> subjects = new Gson().fromJson(yourJsonString, listType);
您的Subject類應該看起來像這樣
class Subject {
int SubjectId;
String SubjectName;
List<Course> Courses;
}
正如柏拉圖所提到的,Json字符串格式不正確,這是我用來測試您想要實現的概念的正確格式。
[{"SubjectId":"1059.0",
"SubjectName":"Accounting",
"Student_CourseId":"0.0",
"UniversitySubjectId":"0.0",
"Courses":[
{"CourseId":"1091.0",
"CourseName":"ACCT 101",
"CourseDescription":"",
"Subject_SubjectId":"1059.0"
},
{"CourseId":"1092.0",
"CourseName":"ACCT 111",
"CourseDescription":"",
"Subject_SubjectId":"1059.0"
}
]
}]
這個gson庫可以使用后,
Gson gson = new Gson();
Subject[] subject = gson.fromJson(jsonString, Subject[].class);
ArrayList<Course> course = subject[0].Courses;
System.out.println(subject[0].SubjectId);
System.out.println(course.get(0).CourseId);
當然,以上邏輯僅在Json格式正確,更正從服務器接收的json響應后才有效,然后才應該起作用。
希望這可以幫助!!!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.