F
F
Fedor unknown2021-01-28 23:09:06
Java
Fedor unknown, 2021-01-28 23:09:06

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

I decided to read the json-File, then assign the data to the String, then add the String to the List, and then do the manipulations described in the task.
Question: Is this a dumb decision? if yes, how to solve this problem?
my code :
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();
        }
}


json file:
{
  "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

2 answer(s)
O
Orkhan, 2021-01-28
Hasanly @azerphoenix

Hello!

Question: Is this a dumb decision? if yes, how to solve this problem?

Rather, you went a very long way.
Here is the structure of your json.
{
  "movies": [
    {
      "name": "Good omens",
      "year": 2019,
      "description": "TV Series",
      "director": {
        "fullName": "Douglas Mackinnon"
      },
      "cast": [
        {
          "fullName": "Michael Sheen",
          "role": "Aziraphale"
        },
        {
          "fullName": "David Tennant",
          "role": "Crowley"
        }
      ]
    }
  ]
}

It's better to read it directly into a list of objects
You can map json into an object using this site - www.jsonschema2pojo.org
It will turn out something like this:
-----------------------------------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;

}

Read for example this article how to read json into object
https://mkyong.com/java/how-to-parse-json-with-gson/

D
Dmitry Roo, 2021-01-28
@xez

Is this a dumb decision?

Not the most optimal. And not object-oriented.
You need to:
1. Create a model from your json file.
2. Overtake, using gson, for example, a file into this model. You should get some kind of list from movie.
3. Perform actions with this list. An excellent opportunity to understand the Stream API.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question