I'm new to android developing and created an "eventlogger" app where you start by choosing an infinite number of Events in this Activity:
我是Android新手,开发并创建了一个“eventlogger”应用程序,您可以在此活动中选择无限数量的事件:
All the Events are added into an ArrayList called "events" that looks like that:
所有事件都被添加到名为“events”的ArrayList中,如下所示:
[Event 1, Event 2, Event 3]
In the next Activity you can start a timer, press the buttons and log the times as strings into an Arraylist of Arraylists.
在下一个Activity中,你可以启动一个计时器,按下按钮并将时间作为字符串记录到Arraylists的Arraylist中。
The ArrayList of Arraylists is named "allEventsList" is created
Arraylists的ArrayList被命名为“allEventsList”被创建
ArrayList<ArrayList<String>> allActionsList;
and then filled with data (times), so it looks like that
然后填充数据(次),所以它看起来像那样
[[00:00:01, 00:00:09, 00:00:12], [00:00:04, 00:00:07, 00:00:14], [00:00:03, 00:00:06, 00:00:15]]
At the end I want to safe that in a .json file that looks like that
最后,我想在一个看起来像这样的.json文件中保护它
{
"events":{
"Event 1":["00:00:01", "00:00:09", "00:00:12"],
"Event 2":["00:00:04", "00:00:07", "00:00:14"],
"Event 3":["00:00:03", "00:00:06", "00:00:15"]
}
How can I write my data in a .json file and how can I retrieve it later on and where should I store it?
如何在.json文件中编写数据,如何在以后检索它以及在哪里存储它?
1 个解决方案
#1
2
Here you have a way to do it:
在这里你有办法做到这一点:
private String convertToJsonString(ArrayList<ArrayList<String>> allActionsList)
{
JSONObject jObj = new JSONObject();
JSONObject events = new JSONObject();
JSONArray eventX;
int count = 1;
String key;
try {
for(ArrayList<String> array : allActionsList){
key = String.format("Event %d", count);
eventX = new JSONArray();
for(String s : array){
eventX.put(s);
}
events.put(key, eventX);
count++;
}
jObj.put("events", events);
} catch(Exception e){
// failed somewhere to create JSON object
e.printStackTrace();
}
return jObj.toString();
}
After converting to String you can just save it to a file.
转换为String后,您只需将其保存到文件即可。
Then you can read it back from file as a string and build your JSON object back like this:
然后你可以将它作为字符串从文件中读回来并像这样构建你的JSON对象:
JSONObject events = new JSONObject(<string read from file>);
Hope this works for you.
希望这对你有用。
#1
2
Here you have a way to do it:
在这里你有办法做到这一点:
private String convertToJsonString(ArrayList<ArrayList<String>> allActionsList)
{
JSONObject jObj = new JSONObject();
JSONObject events = new JSONObject();
JSONArray eventX;
int count = 1;
String key;
try {
for(ArrayList<String> array : allActionsList){
key = String.format("Event %d", count);
eventX = new JSONArray();
for(String s : array){
eventX.put(s);
}
events.put(key, eventX);
count++;
}
jObj.put("events", events);
} catch(Exception e){
// failed somewhere to create JSON object
e.printStackTrace();
}
return jObj.toString();
}
After converting to String you can just save it to a file.
转换为String后,您只需将其保存到文件即可。
Then you can read it back from file as a string and build your JSON object back like this:
然后你可以将它作为字符串从文件中读回来并像这样构建你的JSON对象:
JSONObject events = new JSONObject(<string read from file>);
Hope this works for you.
希望这对你有用。