简体   繁体   English

在Android中存储嵌套静态数据(数组)的最佳方法?

[英]Best way to store nested static data(array) in Android?

So I have data, for example: 所以我有数据,例如:

1 topic will be conducted in 2 class room with different subtopic each hour from 9am-12pm. 从上午9点至下午12点,每小时将在2个带有不同子主题的课堂中进行1个主题。

It will look like below if in JSON format: 如果为JSON格式,则如下所示:

{
    "Mathematic": [
        {
            "time": [
                {
                    "startTime": "09.00",
                    "endTime": "10.00",
                    "class": [
                        {
                            "subtopic": "What is Calculus?",
                            "roomName": "Class A"
                        },
                        {
                            "subtopic": "Basic of Calculus",
                            "roomName": "Class B"
                        }
                    ]
                }
            ]
        }
    ]
}

If I put the json file in assets folder, I believe the JSON parser will take some time to parse it. 如果我将json文件放在assets文件夹中,我相信JSON解析器将需要一些时间来解析它。

I also think about to put it in arrays.xml : 我也考虑将其放在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>

I am not sure how to get the nested array, here is my code: 我不确定如何获取嵌套数组,这是我的代码:

String [] test = getResources().getStringArray(R.array.testArray);
for(int i = 0; i < test.length; i++)
    System.out.println("test: " + test[i]);

Output: 输出:

test: first
test: second
test:    first nested   second nested   
test: fourth
test: fifth

What is the best way to store nested static data? 存储嵌套静态数据的最佳方法是什么?

In order to meet your JSON, this type of strcutures will be needed. 为了满足您的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;
}

But if you can change that json 但是如果您可以更改该json

I would suggest 我会建议

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM