public static void runClient() throws JSONException {
String httpServerHostAddr = "127.0.0.1";
int httpServerPort = Constants.HTTP_SERVER_PORT;
ProductResource clientHandle = ClientResource.create(
"http://" + httpServerHostAddr + ":" + httpServerPort + "/student", ProductResource.class);
StudentDTO student = new StudentDTO();
System.out.println("TEST Starting\n");
student.setStudentNo("123456789");
student.setName("Jhon");
student.setSurname("Gibson");
student.setIdentityNumber("15423200");
student.setSex(Byte.valueOf("0"));
student.setFacultyId(Byte.valueOf("1"));
student.setDepartmentId(Byte.valueOf("2"));
JsonRepresentation jsonRep = student.toJsonRepresentation();
JsonRepresentation response = (JsonRepresentation) clientHandle.store(jsonRep);
StudentDTO responseStudent = new StudentDTO(response);
System.out.println("What's Stored: " + responseStudent.toString());
ProductResource clientHandle2 = ClientResource.create(
"http://" + httpServerHostAddr + ":" + httpServerPort + "/student/"
+ responseStudent.getStudentId(), ProductResource.class);
response = (JsonRepresentation) clientHandle2.represent();
responseStudent = new StudentDTO(response);
System.out.println("\nWhat's retrieved: " + responseStudent.toString());
以上代码是我测试项目的主要代码
@Override
public JsonRepresentation toJsonRepresentation() throws SystemException {
try {
JSONObject jsonObjectRep = new JSONObject();
if (this.getExitCode() == Constants.EXIT_SUCCESS) {
jsonObjectRep.put("studentId", this.getStudentId());
jsonObjectRep.put("studentNo", this.getStudentNo());
jsonObjectRep.put("name", this.getName());
jsonObjectRep.put("surname", this.getSurname());
jsonObjectRep.put("identityNumber", this.getIdentityNumber());
jsonObjectRep.put("birthDate",this.getBirthDate());
jsonObjectRep.put("sex", this.getSex());
jsonObjectRep.put("facultyId", this.getFacultyId());
jsonObjectRep.put("departmentId", this.getDepartmentId());
jsonObjectRep.put("educationYear", this.getEducationYear());
jsonObjectRep.put("educationStatus", this.getEducationStatus());
jsonObjectRep.put("macAdress", this.getMacAdress());
jsonObjectRep.put("exitCode", this.getExitCode());
} else {
jsonObjectRep.put("exitCode", this.getExitCode());
}
JsonRepresentation jsonRep = new JsonRepresentation(jsonObjectRep);
return jsonRep;
} catch (JSONException ex) {
SystemException e = SystemException.wrap(ex, ValidationCode.UNABLE_TO_PRODUCE_JSON_REPRESENTATION);
throw e;
}
}
上面的代码片段显示了我如何创建我的JSON对象
@Override
protected void fromJsonRepresentation(JsonRepresentation json) throws SystemException {
try {
JSONObject jsonObj;
jsonObj = json.getJsonObject();
this.studentId = jsonObj.getInt("studentId");
this.studentNo = jsonObj.getString("studentNo");
this.name = jsonObj.getString("name");
this.surname = jsonObj.getString("surname");
this.identityNumber = jsonObj.getString("identityNumber");
try {
this.birthdate = sFormat.parse(jsonObj.getString("birthDate"));
} catch (ParseException ex) {
Logger.getLogger(StudentDTO.class.getName()).log(Level.SEVERE, null, ex);
}
this.sex = (byte) jsonObj.getInt("sex");
this.facultyId = (byte) jsonObj.getInt("facultyId");
this.departmentId = (byte) jsonObj.getInt("departmentId");
this.educationYear = (byte) jsonObj.getInt("educationYear");
this.educationStatus = (byte) jsonObj.getInt("educationStatus");
this.macAdress = jsonObj.getString("macAdress");
this.setExitCode(jsonObj.getInt("exitCode"));
} catch (JSONException ex) {
SystemException e = SystemException.wrap(ex, ValidationCode.UNABLE_TO_PARSE_JSON);
throw e;
}
}
这就是我在尝试解析代码中的JSON对象。我收到此错误org.json.JSONException: JSONObject["studentId"] not found.