简体   繁体   中英

Better way to organize the following snippet by avoiding too many json.getString

Consider the following snippet

public static JSONObject getStudentDetails() throws Exception {
//returns the details as json
}

public static JSONObject getDepartmentDetails(String department_id) throws Exception {
//returns the details as json
}

public void doPost(HttpServletRequest req, HttpServletResponse res) throws Exception {

JSONObject json1 = getStudentDetails();
String name = json1.getString(“name”);
String role_number = json1.getString(“role_number”);
String department_id = json1.getStrring(“department_id”);
//and goes on - to get all student details


JSONObject json2 = getDepartmentDetails(department_id);
String department_hod = json2.getString(“department_hod”);
String department_name = json2.getString(“department_name”);
String department_block = json2.getString(“department_block”);
//and goes on - till i get all the department details

//Followed by main action to be performed
}

In the above code for most of the case i am likely to call an util method which returns a json object which i am using in my doGet/doPost method. So most part of my code in my doPost/doGet is like to fetch values from json object with getString . Is there any better way to organise the above snippet?

I suggest your start using JSON parser library. There are some JSON parser libraries that famous out there, for example: FastJSON, Jackson, Gson, etc.

If you don't want to use any external libraries, for cleaner code, you better to create a model to put your data there, and be sure as clear as possible when naming your variables. Because there is no way to avoid json.getString without the help of external libraries .

class Student{
    private String name;
    private String roleNumber;
    private String departmentId;

    public String getName() {
        return name;
    }

    public Student setName(String name) {
        this.name = name;
        return this;
    }

    public String getRoleNumber() {
        return roleNumber;
    }

    public Student setRoleNumber(String roleNumber) {
        this.roleNumber = roleNumber;
        return this;
    }

    public String getDepartmentId() {
        return departmentId;
    }

    public Student setDepartmentId(String departmentId) {
        this.departmentId = departmentId;
        return this;
    }
}

public void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
    JSONObject studentJsonObject = getStudentDetails();
    Student student = new Student()
            .setName(studentJsonObject.getString("name"))
            .setRoleNumber(studentJsonObject.getString("role_number"))
            .setDepartmentId(studentJsonObject.getString("department_id"));

    //then you can easily get each property of student
    //like student.getName(); or student.getDepartmentId();

    //the rest of your code
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM