Reputation: 45
I am looking for a way to handle the information that I receive in a JSON file. This file is supposed to change constantly (other people and programs are going to access it), so in a moment it would be like this example:
{
"activities": [
{
"name": "do my homework",
"status": "not yet done",
"important": true
},
{
"name": "wash the dishes",
"status": "not yet done",
"important": true
},
{
"name": "play videogames",
"status": "done",
"important": false
},
{
"name": "take a shower",
"status": "done",
"important": true
}
]
}
And 15 minutes later (just to say an amount of time) it would be like this
{
"activities": [
{
"name": "do my homework",
"status": "done", //NOTICE THIS ACTIVITY WAS DONE
"important": true
},
{
"name": "wash the dishes",
"status": "not yet done",
"important": false //NOTICE THIS HAS CHANGED ITS IMPORTANCE
},
{
"name": "play videogames",
"status": "done",
"important": false
},
{
"name": "take a shower",
"status": "done",
"important": true
},
{ //NOTICE I'VE ADDED A NEW ACTIVITY
"name": "take another shower",
"status": "not yet done",
"important": false
}
]
}
What I want to know is how to handle these changes in Java (at least an idea) that could help me to detect these changes and handle them.
I was thinking about creating two arraylists (doneActivities and notYetDoneActivities) and every 10 minutes call the json file and check if there were changes, and probably switch objects between those arraylists (sounds fine, but I don't know how could I do it).
Or I could even use a mysql database, but I think it could further get my code more complex and I'm trying to keep it as simple as possible.
notice that my Activities class is like this:
public class Activity {
private String name;
private boolean done;
private boolean important;
//Getters and setters
}
Any suggestions will be really appreciated.
Upvotes: 0
Views: 722
Reputation: 1208
You need an identity field - something that is unique to each Activity, so that you can always tell apart the ones you've already processed from any new ones that appear. You haven't mentioned how the json data is generated, but if there's a database involved then it probably already has an id stored that you can include in the json.
public class Activity {
private long id;
private String name;
private boolean done;
private boolean important;
//Getters and setters
@Override
public boolean equals(Object o) {
if (o instanceof Activity) {
Activity other = (Activity) o;
return this.getId() == other.getId();
}
return false;
}
}
The equals method will allow you to store your Activities in a set, e.g. HashSet
Upvotes: 3
Reputation: 2037
First you will have to use something to marshall/unmarshall your JSON to Java, i recommend GSON, here is a link to a tutorial: https://sites.google.com/site/gson/gson-user-guide
You will use someline like that:
Activityies fullObject = gson.fromJson(json, Activities.class);
Where it contains a list of Activity: List<Activity>
Then i would recommend overwriting the toString
method to compare the l it easier, but you can implement some logic to compare if your list is different.
Upvotes: 0