[英]Best way to store nested static data(array) in Android?
所以我有數據,例如:
從上午9點至下午12點,每小時將在2個帶有不同子主題的課堂中進行1個主題。
如果為JSON格式,則如下所示:
{
"Mathematic": [
{
"time": [
{
"startTime": "09.00",
"endTime": "10.00",
"class": [
{
"subtopic": "What is Calculus?",
"roomName": "Class A"
},
{
"subtopic": "Basic of Calculus",
"roomName": "Class B"
}
]
}
]
}
]
}
如果我將json文件放在assets
文件夾中,我相信JSON解析器將需要一些時間來解析它。
我也考慮將其放在arrays.xml
:
<array name="testArray">
<item>first</item>
<item>second</item>
<item>
<array name="testArrayNested">
<item>
first nested
</item>
<item>
second nested
</item>
</array>
</item>
<item>fourth</item>
<item>fifth</item>
</array>
我不確定如何獲取嵌套數組,這是我的代碼:
String [] test = getResources().getStringArray(R.array.testArray);
for(int i = 0; i < test.length; i++)
System.out.println("test: " + test[i]);
輸出:
test: first
test: second
test: first nested second nested
test: fourth
test: fifth
存儲嵌套靜態數據的最佳方法是什么?
為了滿足您的JSON,將需要這種類型的結構。
class Lecture { //Mathematic
String name;
List<Timetable> timetables;
}
class Timetable { //time
Time start;
Time end;
List<Lesson> classes;
}
class Lesson { //class
ClassRoom room;
Topic topic;
}
class Topic {
String name;
}
class ClassRoom {
String name;
}
但是如果您可以更改該json
我會建議
class Subject {
String topic;
}
class Lesson {
Subject subject;
String topic;
}
class Classroom {
String name;
}
class ClassroomLesson {
Lesson lesson;
Classroom classroom;
String hour;
}
class Schedule {
List<ClassroomLesson> lessons = new ArrayList<>();
}
{
"lessons": [
{
"lesson": {
"subject": {
"topic": "Mathematics"
},
"topic": "Lesson 1"
},
"classroom": {
"name": "Classroom 1"
},
"hour": "09:00"
},
{
"lesson": {
"subject": {
"topic": "Mathematics"
},
"topic": "Lesson 2"
},
"classroom": {
"name": "Classroom 2"
},
"hour": "10:00"
}
]
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.