Reputation: 11
I have a java class named Teacher:
public class Teacher {
private String _tCode;
private String _tName;
private String[] _teacherLessons;
private int _maxHoursPerDay;
private int _maxHoursPerWeek;
public String get_T_Code(){return this._tCode;}
public String get_T_Name(){return this._tName;}
public String[] get_Teacher_Lessons(){return this._teacherLessons;}
public int get_Max_Hours_Per_Day(){return this._maxHoursPerDay;}
public int get_Max_Hours_Per_Week(){return this._maxHoursPerWeek;}
public void set_T_Code(String tCode){this._tCode=tCode;}
public void set_T_Name(String tName){this._tName=tName;}
public void set_Teacher_Lessons(String[] teacherLessons){this._teacherLessons =teacherLessons;}
public void set_Max_Hours_Per_Day(int maxHoursPerDay){this._maxHoursPerDay=maxHoursPerDay;}
public void set_Max_Hours_Per_Week(int maxHoursPerWeek){this._maxHoursPerWeek=maxHoursPerWeek;}
public void teacher (String tCode, String tName, String[] teacherLessons, int maxHoursPerDay, int maxHoursPerWeek){
this._tCode=tCode;
this._tName=tName;
this._teacherLessons=teacherLessons;
this._maxHoursPerDay=maxHoursPerDay;
this._maxHoursPerWeek=maxHoursPerWeek;
}
@Override
public String toString(){ return "Code:\t"+this._tCode+"\nName:\t"+this._tName+"\nTeacher Lessons:\t"+this._teacherLessons+
"\nMax hours per day:\t"+this._maxHoursPerDay+"\nMax hours per week:\t"+this._maxHoursPerWeek;}
}
I want my main class to read from a json file and create Teacher objects but I am not sure what is the best way to write my data into json file. Here is what I have tried so far:
[{
"Teacher Code": "1",
"Teacher Name": "john",
"MaxHoursPerDay":5,
"MaxHoursPerWeek":18,
"Teacher Lessons": [
"Maths"
]
},
{
"Teacher Code": "2",
"Teacher Name": "Bill",
"MaxHoursPerDay":5,
"MaxHoursPerWeek":18,
"Teacher Lessons": [
"Maths",
"Physics
]
}]
I create objects like this:
JSONArray a = (JSONArray) parser.parse(new FileReader("c:\\file1.txt"));
for (Object o : a)
{
JSONObject person = (JSONObject) o;
String code= (String) person.get("code");
String name= (String) person.get("name");
int maxHoursD= (int) person.get("MaxHourPerDay");
int maxHoursW= (int) person.get("MaxHourPerWeek");
JSONArray lessons = (JSONArray) person.get("Teacher Lessons");
for (Object c : lessons)
{
Teacher t = new Teacher(code, name, maxHoursD, maxHoursW, c);
}
}
Is there easier way to read the json data and create objects? Moreover, is it possible to store json array(Teacher lessons) in a java array as in my constructor?
Upvotes: 0
Views: 4443
Reputation: 1058
Google Gson is a pretty good thrid party library that can easily be used along with your code. It does exactly what you say you want to do. Convert objects to JSON and vice versa.
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public static void main(String[] args) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
Teacher teacher = new Teacher("001", "John Doe", 8, 40, "History", "Civics");
System.out.println("Teacher Object :\n" + gson.toJson(teacher));
Teacher teacher1 = new Teacher("002", "Ali G", 8, 40, "English Literature", "English Language");
Teachers teachers = new Teachers(teacher, teacher1);
System.out.println("Teachers Object :\n" + gson.toJson(teachers));
}
public static class Teachers {
private Teacher[] teachers;
public Teachers(Teacher... teachers) {
this.teachers = teachers;
}
}
public static class Teacher {
private String _tCode;
private String _tName;
private String[] _teacherLessons;
private int _maxHoursPerDay;
private int _maxHoursPerWeek;
public Teacher(String _tCode, String _tName, int _maxHoursPerDay, int _maxHoursPerWeek,
String... _teacherLessons) {
this._tCode = _tCode;
this._tName = _tName;
this._teacherLessons = _teacherLessons;
this._maxHoursPerDay = _maxHoursPerDay;
this._maxHoursPerWeek = _maxHoursPerWeek;
this._teacherLessons = _teacherLessons;
}
}
The results of the above bit of code is as follows :
Teacher Object :
{
"_tCode": "001",
"_tName": "John Doe",
"_teacherLessons": [
"History",
"Civics"
],
"_maxHoursPerDay": 8,
"_maxHoursPerWeek": 40
}
Teachers Object :
{
"teachers": [
{
"_tCode": "001",
"_tName": "John Doe",
"_teacherLessons": [
"History",
"Civics"
],
"_maxHoursPerDay": 8,
"_maxHoursPerWeek": 40
},
{
"_tCode": "002",
"_tName": "Ali G",
"_teacherLessons": [
"English Literature",
"English Language"
],
"_maxHoursPerDay": 8,
"_maxHoursPerWeek": 40
}
]
}
The output should tell you exactly how you should structure your JSON in accordance with your model object.
Upvotes: 2