Answer the question
In order to leave comments, you need to log in
Is it reasonable to read the json file and assign it to a string, then a string to an Arraylist?
Hello!
I have a task:
movie data is stored in a JSON file ( movies.json ). You need to write a program that reads it from a file and can perform the following actions with it:
Display a collection of movies on the screen;
Search and display movies by full and partial match in the title.
Sort the entire collection of films in ascending and descending order by the following fields:
by year of release of the film
by title
by director
public class Movies implements Actionable{
Gson gson = new GsonBuilder().setPrettyPrinting().create();
Scanner sc;
private String str;
private List<String> list = new ArrayList<>();
public void readJson(){
try {
FileReader fileReader = new FileReader("movies.json");
sc = new Scanner(fileReader);
while (sc.hasNextLine()){
str += sc.nextLine(); // записать данные из файла в строку str
}
fileReader.close();
sc.close();
list.add(str); // далее добавить строку в list
}catch (IOException exception){
exception.printStackTrace();
}
}
{
"movies": [
{
"name": "Good omens",
"year": 2019,
"description": "TV Series",
"director": {
"fullName": "Douglas Mackinnon"
},
"cast": [
{
"fullName": "Michael Sheen",
"role": "Aziraphale"
},
{
"fullName": "David Tennant",
"role": "Crowley"
}
]
},
Answer the question
In order to leave comments, you need to log in
Hello!
Question: Is this a dumb decision? if yes, how to solve this problem?
{
"movies": [
{
"name": "Good omens",
"year": 2019,
"description": "TV Series",
"director": {
"fullName": "Douglas Mackinnon"
},
"cast": [
{
"fullName": "Michael Sheen",
"role": "Aziraphale"
},
{
"fullName": "David Tennant",
"role": "Crowley"
}
]
}
]
}
-----------------------------------com.example.Example.java-----------------------------------
public class Example {
@SerializedName("movies")
@Expose
public List<Movie> movies = null;
}
-----------------------------------com.example.Movie.java-----------------------------------
public class Movie {
@SerializedName("name")
@Expose
public String name;
@SerializedName("year")
@Expose
public Integer year;
@SerializedName("description")
@Expose
public String description;
@SerializedName("director")
@Expose
public Director director;
@SerializedName("cast")
@Expose
public List<Cast> cast = null;
}
-----------------------------------com.example.Director.java-----------------------------------
public class Director {
@SerializedName("fullName")
@Expose
public String fullName;
}
-----------------------------------com.example.Cast.java-----------------------------------
public class Cast {
@SerializedName("fullName")
@Expose
public String fullName;
@SerializedName("role")
@Expose
public String role;
}
Is this a dumb decision?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question