Home java reading json from file using gson

reading json from file using gson

Author

Date

Category

Trying to tritely read the JSON file, followed by creating a class object, but nothing comes out, BufferedReader does not even read the file, although in the examples it is directly transmitted to the GSON, dependency in the POM file added ..

JSON itself of this format

{
 "Student": [{
  "Name": "Ivan",
  "SURNAME": "Ivanov",
  }, {
   "Name": "Peter",
  "SURNAME": "Ivanov",
 }]
}

created two classes

Public Class Pojostudents
{
   String Name;
   String Surname;
}

and

Public Class GeneralStudents {
  List & lt; Pojostudents & gt; General;
}

Class Main

import java.io.bufferedReader;
Import java.io.FileReader;
Import com.google.gson.gson;
  Public Class Main.
  {
    Public Static Void Main (String [] Args) Throws Exception
    {
      GSON GSON = NEW GSON ();
      BufferedReader Br = New BufferedReader (New FileReader ("D: \\ Students.json"));
      GeneralStudents Students = Gson.Fromjson (Br, GeneralStudents.class);
    }
  }

Answer 1, Authority 100%

You have the wrong variable name in the main class:

Public Class GeneralStudents {
  List & lt; Pojostudents & gt; General;
}

should be instead of Student instead of general

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions